Skip to content

Commit

Permalink
[tuya-connector] Connector cleanup (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Nov 24, 2023
1 parent f1b4ca5 commit 1ff3cb1
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 146 deletions.
31 changes: 14 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<p align="center">
<img src="https://github.com/fastybird/.github/blob/main/assets/repo_title.png?raw=true" alt="FastyBird"/>
</p>

# FastyBird IoT Tuya connector

[![Build Status](https://badgen.net/github/checks/FastyBird/tuya-connector/main?cache=300&style=flast-square)](https://github.com/FastyBird/tuya-connector/actions)
[![Licence](https://badgen.net/github/license/FastyBird/tuya-connector?cache=300&style=flast-square)](https://github.com/FastyBird/tuya-connector/blob/main/LICENSE.md)
[![Code coverage](https://badgen.net/coveralls/c/github/FastyBird/tuya-connector?cache=300&style=flast-square)](https://coveralls.io/r/FastyBird/tuya-connector)
[![Build Status](https://img.shields.io/github/actions/workflow/status/FastyBird/tuya-connector/ci.yaml?style=flat-square)](https://github.com/FastyBird/tuya-connector/actions)
[![Licence](https://img.shields.io/github/license/FastyBird/tuya-connector?style=flat-square)](https://github.com/FastyBird/tuya-connector/blob/main/LICENSE.md)
[![Code coverage](https://img.shields.io/coverallsCoverage/github/FastyBird/tuya-connector?style=flat-square)](https://coveralls.io/r/FastyBird/tuya-connector)
[![Mutation testing](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2FFastyBird%2Ftuya-connector%2Fmain)](https://dashboard.stryker-mutator.io/reports/github.com/FastyBird/tuya-connector/main)

![PHP](https://badgen.net/packagist/php/FastyBird/tuya-connector?cache=300&style=flast-square)
[![Latest stable](https://badgen.net/packagist/v/FastyBird/tuya-connector/latest?cache=300&style=flast-square)](https://packagist.org/packages/FastyBird/tuya-connector)
[![Downloads total](https://badgen.net/packagist/dt/FastyBird/tuya-connector?cache=300&style=flast-square)](https://packagist.org/packages/FastyBird/tuya-connector)
![PHP](https://badgen.net/packagist/php/FastyBird/tuya-connector?cache=300&style=flat-square)
[![Latest stable](https://badgen.net/packagist/v/FastyBird/tuya-connector/latest?cache=300&style=flat-square)](https://packagist.org/packages/FastyBird/tuya-connector)
[![Downloads total](https://badgen.net/packagist/dt/FastyBird/tuya-connector?cache=300&style=flat-square)](https://packagist.org/packages/FastyBird/tuya-connector)
[![PHPStan](https://img.shields.io/badge/PHPStan-enabled-brightgreen.svg?style=flat-square)](https://github.com/phpstan/phpstan)

***
Expand All @@ -17,9 +21,6 @@
Tuya connector is extension for [FastyBird](https://www.fastybird.com) [IoT](https://en.wikipedia.org/wiki/Internet_of_things) ecosystem
which is integrating [Tuya](https://www.tuya.com) devices.

Tuya Connector is a distributed extension that is developed in [PHP](https://www.php.net), built on the [Nette](https://nette.org) and [Symfony](https://symfony.com) frameworks,
and is licensed under [Apache2](http://www.apache.org/licenses/LICENSE-2.0).

### Features:

- The Tuya Connector offers support for both local and cloud-based communication with Tuya devices, providing users with a versatile and flexible way to connect and control a wide range of Tuya devices in their home or office.
Expand All @@ -29,6 +30,8 @@ and is licensed under [Apache2](http://www.apache.org/licenses/LICENSE-2.0).
- [{JSON:API}](https://jsonapi.org/) schemas for full API access, providing a standardized and consistent way for developers to access and manipulate Tuya device data
- Regular updates with new features and bug fixes, ensuring that the Tuya Connector is always up-to-date and reliable.

Tuya Connector is a distributed extension that is developed in [PHP](https://www.php.net), built on the [Nette](https://nette.org) and [Symfony](https://symfony.com) frameworks,
and is licensed under [Apache2](http://www.apache.org/licenses/LICENSE-2.0).

## Requirements

Expand All @@ -37,19 +40,13 @@ PHP extension.

## Installation

### Manual installation

The best way to install **fastybird/tuya-connector** is using [Composer](http://getcomposer.org/):
This extension is part of the [FastyBird](https://www.fastybird.com) [IoT](https://en.wikipedia.org/wiki/Internet_of_things) ecosystem and is installed by default.
In case you want to create you own distribution of [FastyBird](https://www.fastybird.com) [IoT](https://en.wikipedia.org/wiki/Internet_of_things) ecosystem you could install this extension with [Composer](http://getcomposer.org/):

```sh
composer require fastybird/tuya-connector
```

### Marketplace installation [WIP]

You could install this connector in your [FastyBird](https://www.fastybird.com) [IoT](https://en.wikipedia.org/wiki/Internet_of_things)
application under marketplace section.

## Documentation

Learn how to connect Tuya devices and manage them with [FastyBird](https://www.fastybird.com) [IoT](https://en.wikipedia.org/wiki/Internet_of_things) system
Expand Down
17 changes: 13 additions & 4 deletions docs/Home.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<p align="center">
<img src="https://github.com/fastybird/.github/blob/main/assets/repo_title.png?raw=true" alt="FastyBird"/>
</p>

The [FastyBird](https://www.fastybird.com) [IoT](https://en.wikipedia.org/wiki/Internet_of_things) Tuya Connector is an extension for the [FastyBird](https://www.fastybird.com) [IoT](https://en.wikipedia.org/wiki/Internet_of_things) ecosystem that enables seamless integration
with [Tuya](https://www.tuya.com) devices. It allows users to easily connect and control [Tuya](https://www.tuya.com) devices from within the [FastyBird](https://www.fastybird.com) [IoT](https://en.wikipedia.org/wiki/Internet_of_things) ecosystem,
providing a simple and user-friendly interface for managing and monitoring your devices.
Expand Down Expand Up @@ -85,6 +89,10 @@ is finished, you will see your devices in the Devices list.

![Application code](https://github.com/FastyBird/tuya-connector/blob/main/docs/_media/tuya_scan_code.png)

After your devices are linked with your Tuya cloud account you are able to get Tuya user identifier.

![User identifier](https://github.com/FastyBird/tuya-connector/blob/main/docs/_media/tuya_cloud_user_id.png)

## Configuring the Connector through the Console

To configure the connector through the console, run the following command:
Expand All @@ -111,19 +119,20 @@ Tuya connector - initialization
You will then be prompted to choose an action:

```shell
What would you like to do?:
What would you like to do? [Nothing]:
[0] Create new connector configuration
[1] Edit existing connector configuration
[2] Delete existing connector configuration
> 0
[3] List Tuya connectors
[4] Nothing
```

If you choose to create a new connector, you will be asked to choose the mode in which the connector will communicate with the devices:

```shell
In what mode should this connector communicate with devices? [Local network mode]:
In what mode should this connector communicate with Tuya devices? [Tuya cloud server mode]:
[0] Local network mode
[1] Cloud server mode
[1] Tuya cloud server mode
> 0
```

Expand Down
Binary file added docs/_media/tuya_cloud_user_id.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/API/LocalApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function __construct(
}

/**
* @return Promise\PromiseInterface<bool>
* @return Promise\PromiseInterface<true>
*/
public function connect(): Promise\PromiseInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/Clients/Discovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function () use ($deferred, $protocolVersion): void {
$this->discoveredLocalDevices = new SplObjectStorage();

if ($devices !== []) {
$devices = $this->handleFoundLocalDevices($devices);
$this->handleFoundLocalDevices($devices);
}

$this->emit('finished', [$devices]);
Expand Down
58 changes: 30 additions & 28 deletions src/Queue/Consumers/ChannelProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,50 +95,50 @@ function () use ($property): void {
return;
}

if (
$property instanceof DevicesEntities\Channels\Properties\Variable
&& $property->getValue() === $value
) {
return;
}

if ($property !== null && !$property instanceof $type) {
$findChannelPropertyQuery = new DevicesQueries\Entities\FindChannelProperties();
$findChannelPropertyQuery->byId($property->getId());
$this->databaseHelper->transaction(function () use ($property): void {
$this->channelsPropertiesManager->delete($property);
});

$property = $this->channelsPropertiesRepository->findOneBy($findChannelPropertyQuery);
$this->logger->warning(
'Stored channel property was not of valid type',
[
'source' => MetadataTypes\ConnectorSource::SOURCE_CONNECTOR_TUYA,
'type' => 'message-consumer',
'channel' => [
'id' => $channelId->toString(),
],
'property' => [
'id' => $property->getId()->toString(),
'identifier' => $identifier,
],
],
);

if ($property !== null) {
$this->databaseHelper->transaction(function () use ($property): void {
$this->channelsPropertiesManager->delete($property);
});
$property = null;
}

$this->logger->warning(
'Stored channel property was not of valid type',
if ($property === null) {
$findChannelQuery = new DevicesQueries\Entities\FindChannels();
$findChannelQuery->byId($channelId);

$channel = $this->channelsRepository->findOneBy($findChannelQuery);

if ($channel === null) {
$this->logger->error(
'Channel was not found, property could not be configured',
[
'source' => MetadataTypes\ConnectorSource::SOURCE_CONNECTOR_TUYA,
'type' => 'message-consumer',
'channel' => [
'id' => $channelId->toString(),
],
'property' => [
'id' => $property->getId()->toString(),
'identifier' => $identifier,
],
],
);
}

$property = null;
}

if ($property === null) {
$findChannelQuery = new DevicesQueries\Entities\FindChannels();
$findChannelQuery->byId($channelId);

$channel = $this->channelsRepository->findOneBy($findChannelQuery);

if ($channel === null) {
return;
}

Expand Down Expand Up @@ -186,6 +186,8 @@ function () use ($property): void {

} else {
if ($property instanceof DevicesEntities\Channels\Properties\Dynamic) {
// Some Tuya devices has invalid values configured
// Eg. wifi dimmable device has allowed values "incandescent" and "halogen" but it also provide "led" value
$findPropertyQuery = new DevicesQueries\Configuration\FindChannelDynamicProperties();
$findPropertyQuery->byId($property->getId());

Expand Down
24 changes: 14 additions & 10 deletions src/Queue/Consumers/DeviceProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use FastyBird\Connector\Tuya;
use FastyBird\Connector\Tuya\Entities;
use FastyBird\Connector\Tuya\Queries;
use FastyBird\Library\Metadata\Exceptions as MetadataExceptions;
use FastyBird\Library\Metadata\Types as MetadataTypes;
use FastyBird\Module\Devices\Entities as DevicesEntities;
use FastyBird\Module\Devices\Exceptions as DevicesExceptions;
Expand Down Expand Up @@ -52,8 +51,6 @@ trait DeviceProperty
* @throws DBAL\Exception
* @throws DevicesExceptions\InvalidState
* @throws DevicesExceptions\Runtime
* @throws MetadataExceptions\InvalidArgument
* @throws MetadataExceptions\InvalidState
*/
private function setDeviceProperty(
Uuid\UuidInterface $deviceId,
Expand Down Expand Up @@ -84,13 +81,6 @@ function () use ($property): void {
return;
}

if (
$property instanceof DevicesEntities\Devices\Properties\Variable
&& $property->getValue() === $value
) {
return;
}

if (
$property !== null
&& !$property instanceof DevicesEntities\Devices\Properties\Variable
Expand Down Expand Up @@ -134,6 +124,20 @@ function () use ($property): void {
);

if ($device === null) {
$this->logger->error(
'Device was not found, property could not be configured',
[
'source' => MetadataTypes\ConnectorSource::SOURCE_CONNECTOR_TUYA,
'type' => 'message-consumer',
'device' => [
'id' => $deviceId->toString(),
],
'property' => [
'identifier' => $identifier,
],
],
);

return;
}

Expand Down
26 changes: 0 additions & 26 deletions src/Queue/Consumers/StoreCloudDevice.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use FastyBird\Connector\Tuya\Queue;
use FastyBird\Connector\Tuya\Types;
use FastyBird\Library\Metadata\Documents as MetadataDocuments;
use FastyBird\Library\Metadata\Exceptions as MetadataExceptions;
use FastyBird\Library\Metadata\Types as MetadataTypes;
use FastyBird\Module\Devices\Entities as DevicesEntities;
use FastyBird\Module\Devices\Exceptions as DevicesExceptions;
Expand Down Expand Up @@ -73,8 +72,6 @@ public function __construct(
* @throws DBAL\Exception
* @throws DevicesExceptions\InvalidState
* @throws DevicesExceptions\Runtime
* @throws MetadataExceptions\InvalidArgument
* @throws MetadataExceptions\InvalidState
*/
public function consume(Entities\Messages\Entity $entity): bool
{
Expand Down Expand Up @@ -128,29 +125,6 @@ function () use ($entity, $connector): Entities\TuyaDevice {
'data' => $entity->toArray(),
],
);
} else {
$device = $this->databaseHelper->transaction(
function () use ($entity, $device): Entities\TuyaDevice {
$device = $this->devicesManager->update($device, Utils\ArrayHash::from([
'name' => $entity->getName(),
]));
assert($device instanceof Entities\TuyaDevice);

return $device;
},
);

$this->logger->debug(
'Device was updated',
[
'source' => MetadataTypes\ConnectorSource::SOURCE_CONNECTOR_TUYA,
'type' => 'store-cloud-device-message-consumer',
'device' => [
'id' => $device->getId()->toString(),
],
'data' => $entity->toArray(),
],
);
}

$this->setDeviceProperty(
Expand Down
Loading

0 comments on commit 1ff3cb1

Please sign in to comment.