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

Light fixes and BLE bulbs support #425

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
17 changes: 16 additions & 1 deletion custom_components/localtuya/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from .coordinator import pytuya, TuyaCloudApi
from .core.cloud_api import TUYA_ENDPOINTS
from .core.helpers import templates, get_gateway_by_deviceid, gen_localtuya_entities
from .core.ha_entities.base import DPCode
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to make exceptions for DPS instead remove the current condition for now. If there are DPS without value and can be used then the condition is not always accurate.

from .const import (
ATTR_UPDATED_AT,
CONF_ADD_DEVICE,
Expand Down Expand Up @@ -1094,6 +1095,16 @@ def schema_defaults(schema, dps_list=None, **defaults):
return copy


def __is_special_dp(dp, cloud_dp_codes: dict[str, dict]) -> bool:
if not (dp_data := cloud_dp_codes.get(dp)):
return false
if not (code := dp_data.get("code")):
return false
return code in (
DPCode.COLOUR_DATA_RAW,
DPCode.SCENE_DATA_RAW,
)

def dps_string_list(dps_data: dict[str, dict], cloud_dp_codes: dict[str, dict]) -> list:
"""Return list of friendly DPS values."""
strs = []
Expand All @@ -1102,7 +1113,11 @@ def dps_string_list(dps_data: dict[str, dict], cloud_dp_codes: dict[str, dict])
for dp, func in cloud_dp_codes.items():
# Default Manual dp value is -1, we will replace it if it in cloud.
add_dp = dp not in dps_data or dps_data.get(dp) == -1
if add_dp and ((value := func.get("value")) or value is not None):
if add_dp and (
(value := func.get("value"))
or value is not None
or __is_special_dp(dp, cloud_dp_codes)
):
Copy link
Owner

@xZetsubou xZetsubou Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if add_dp and (
(value := func.get("value"))
or value is not None
or __is_special_dp(dp, cloud_dp_codes)
):
if (value := func.get("value", "null")) or add_dp:

dps_data[dp] = f"{value}, cloud pull"

for dp, value in dps_data.items():
Expand Down
1 change: 1 addition & 0 deletions custom_components/localtuya/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
CONF_MUSIC_MODE = "music_mode"
CONF_SCENE_VALUES = "scene_values"
CONF_SCENE_VALUES_FRIENDLY = "scene_values_friendly"
CONF_WRITE_ONLY = "write_only"

# switch
CONF_CURRENT = "current"
Expand Down
4 changes: 4 additions & 0 deletions custom_components/localtuya/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def __init__(
self._hass_entry: HassLocalTuyaData = hass.data[DOMAIN][entry.entry_id]
self._device_config = DeviceConfig(device_config.copy())
self.id = self._device_config.id
self.write_only = False # Used for BLE bulbs, see LocalTuyaLight

self._status = {}
self._interface = None
Expand Down Expand Up @@ -414,6 +415,9 @@ async def set_status(self):
payload, self._pending_status = self._pending_status.copy(), {}
try:
await self._interface.set_dps(payload, cid=self._node_id)
if self.write_only:
# The device never replies, process its status change now
self.status_updated(payload)
except Exception as ex: # pylint: disable=broad-except
self.debug(f"Failed to set values {payload} --> {ex}", force=True)
elif not self.connected:
Expand Down
2 changes: 2 additions & 0 deletions custom_components/localtuya/core/ha_entities/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class DPCode(StrEnum):
COLOUR_DATA = "colour_data" # Colored light mode
COLOUR_DATA_HSV = "colour_data_hsv" # Colored light mode
COLOUR_DATA_V2 = "colour_data_v2" # Colored light mode
COLOUR_DATA_RAW = "colour_data_raw" # Colored light mode for BLE
COMPRESSOR_COMMAND = "compressor_command"
CONCENTRATION_SET = "concentration_set" # Concentration setting
CONTROL = "control"
Expand Down Expand Up @@ -505,6 +506,7 @@ class DPCode(StrEnum):
SCENE_9 = "scene_9"
SCENE_DATA = "scene_data" # Colored light mode
SCENE_DATA_V2 = "scene_data_v2" # Colored light mode
SCENE_DATA_RAW = "scene_data_raw" # Colored light mode for BLE
SEEK = "seek"
SENS = "sens" # Ikuu SXSEN003PIR IP65 Motion Detector (Wi-Fi)
SENSITIVITY = "sensitivity" # Sensitivity
Expand Down
Loading
Loading