Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for BLE TRV #28

Merged
merged 22 commits into from
Jun 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Attempt fixing unique IDs
  • Loading branch information
forabi committed Jun 7, 2023
commit a0df88fc763cd352d050f575acc0ce2d915cf89e
77 changes: 38 additions & 39 deletions custom_components/tuya_ble/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.entity import DeviceInfo, EntityDescription
from homeassistant.helpers.entity import (
DeviceInfo,
EntityDescription,
generate_entity_id,
)
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
Expand Down Expand Up @@ -73,6 +77,9 @@ def __init__(
self._attr_has_entity_name = True
self._attr_device_info = get_device_info(self._device)
self._attr_unique_id = f"{self._device.device_id}-{description.key}"
self.entity_id = generate_entity_id(
"sensor.{}", self._attr_unique_id, hass=hass
)

@property
def available(self) -> bool:
Expand Down Expand Up @@ -122,8 +129,7 @@ def _async_handle_update(self, updates: list[TuyaBLEDataPoint]) -> None:
info = get_device_product_info(self._device)
if info.fingerbot and info.fingerbot.manual_control != 0:
for update in updates:
if (update.id == info.fingerbot.switch and
update.changed_by_device):
if update.id == info.fingerbot.switch and update.changed_by_device:
self.hass.bus.fire(
FINGERBOT_BUTTON_EVENT,
{
Expand All @@ -145,15 +151,14 @@ def _async_handle_disconnect(self) -> None:
if self._unsub_disconnect is None:
delay: float = SET_DISCONNECTED_DELAY
self._unsub_disconnect = async_call_later(
self.hass,
delay,
self._set_disconnected
self.hass, delay, self._set_disconnected
)


@dataclass
class TuyaBLEData:
"""Data for the Tuya BLE integration."""

title: str
device: TuyaBLEDevice
product: TuyaBLEProductInfo
Expand All @@ -170,24 +175,21 @@ class TuyaBLECategoryInfo:
devices_database: dict[str, TuyaBLECategoryInfo] = {
"co2bj": TuyaBLECategoryInfo(
products={
"59s19z5m": # device product_id
TuyaBLEProductInfo(
"59s19z5m": TuyaBLEProductInfo( # device product_id
name="CO2 Detector",
),
},
),
"ms": TuyaBLECategoryInfo(
products={
"ludzroix": # device product_id
TuyaBLEProductInfo(
"ludzroix": TuyaBLEProductInfo( # device product_id
name="Smart Lock",
),
},
),
"szjqr": TuyaBLECategoryInfo(
products={
"3yqdo5yt": # device product_id
TuyaBLEProductInfo(
"3yqdo5yt": TuyaBLEProductInfo( # device product_id
name="CUBETOUCH 1s",
fingerbot=TuyaBLEFingerbotInfo(
switch=1,
Expand All @@ -198,8 +200,7 @@ class TuyaBLECategoryInfo:
reverse_positions=4,
),
),
"xhf790if": # device product_id
TuyaBLEProductInfo(
"xhf790if": TuyaBLEProductInfo( # device product_id
name="CubeTouch II",
fingerbot=TuyaBLEFingerbotInfo(
switch=1,
Expand All @@ -223,12 +224,18 @@ class TuyaBLECategoryInfo:
reverse_positions=11,
manual_control=17,
),
)
),
),
**dict.fromkeys(
["ltak7e1p", "y6kttvd6", "yrnk7mnn",
"nvr2rocq", "bnt7wajf", "rvdceqjh",
"5xhbk964"], # device product_ids
[
"ltak7e1p",
"y6kttvd6",
"yrnk7mnn",
"nvr2rocq",
"bnt7wajf",
"rvdceqjh",
"5xhbk964",
], # device product_ids
TuyaBLEProductInfo(
name="Fingerbot",
fingerbot=TuyaBLEFingerbotInfo(
Expand All @@ -245,16 +252,14 @@ class TuyaBLECategoryInfo:
),
"wk": TuyaBLECategoryInfo(
products={
"drlajpqc": # device product_id
TuyaBLEProductInfo(
"drlajpqc": TuyaBLEProductInfo( # device product_id
name="Thermostatic Radiator Valve",
),
},
),
"wsdcg": TuyaBLECategoryInfo(
products={
"ojzlzzsw": # device product_id
TuyaBLEProductInfo(
"ojzlzzsw": TuyaBLEProductInfo( # device product_id
name="Soil moisture sensor",
),
},
Expand All @@ -263,8 +268,7 @@ class TuyaBLECategoryInfo:


def get_product_info_by_ids(
category: str,
product_id: str
category: str, product_id: str
) -> TuyaBLEProductInfo | None:
category_info = devices_database.get(category)
if category_info is not None:
Expand All @@ -276,9 +280,7 @@ def get_product_info_by_ids(
return None


def get_device_product_info(
device: TuyaBLEDevice
) -> TuyaBLEProductInfo | None:
def get_device_product_info(device: TuyaBLEDevice) -> TuyaBLEProductInfo | None:
return get_product_info_by_ids(device.category, device.product_id)


Expand All @@ -294,9 +296,7 @@ async def get_device_readable_name(
credentials: TuyaBLEDeviceCredentials | None = None
product_info: TuyaBLEProductInfo | None = None
if manager:
credentials = await manager.get_device_credentials(
discovery_info.address
)
credentials = await manager.get_device_credentials(discovery_info.address)
if credentials:
product_info = get_product_info_by_ids(
credentials.category,
Expand All @@ -313,10 +313,7 @@ async def get_device_readable_name(
def get_device_info(device: TuyaBLEDevice) -> DeviceInfo | None:
product_info = None
if device.category and device.product_id:
product_info = get_product_info_by_ids(
device.category,
device.product_id
)
product_info = get_product_info_by_ids(device.category, device.product_id)
product_name: str
if product_info:
product_name = product_info.name
Expand All @@ -327,18 +324,20 @@ def get_device_info(device: TuyaBLEDevice) -> DeviceInfo | None:
hw_version=device.hardware_version,
identifiers={(DOMAIN, device.address)},
manufacturer=(
product_info.manufacturer if product_info else
DEVICE_DEF_MANUFACTURER
product_info.manufacturer if product_info else DEVICE_DEF_MANUFACTURER
),
model=("%s (%s)") % (
model=("%s (%s)")
% (
device.product_model or product_name,
device.product_id,
),
name=("%s %s") % (
name=("%s %s")
% (
product_name,
get_short_address(device.address),
),
sw_version=("%s (protocol %s)") % (
sw_version=("%s (protocol %s)")
% (
device.device_version,
device.protocol_version,
),
Expand Down