Skip to content

Commit

Permalink
Revert "3.2.1: Fix set dp service (#56)"
Browse files Browse the repository at this point in the history
This reverts commit 43f511e.
  • Loading branch information
xZetsubou committed Nov 2, 2023
1 parent 43f511e commit 73f5171
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
27 changes: 13 additions & 14 deletions custom_components/localtuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
EVENT_HOMEASSISTANT_STOP,
SERVICE_RELOAD,
)
from homeassistant.core import HomeAssistant, Event
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.helpers.event import async_track_time_interval
Expand Down Expand Up @@ -58,7 +58,7 @@
SERVICE_SET_DP_SCHEMA = vol.Schema(
{
vol.Required(CONF_DEVICE_ID): cv.string,
vol.Optional(CONF_DP): int,
vol.Required(CONF_DP): int,
vol.Required(CONF_VALUE): object,
}
)
Expand All @@ -83,23 +83,18 @@ async def _handle_reload(service):
]
await asyncio.gather(*reload_tasks)

async def _handle_set_dp(event: Event):
async def _handle_set_dp(event):
"""Handle set_dp service call."""
dev_id = event.data[CONF_DEVICE_ID]
entry: ConfigEntry = async_config_entry_by_device_id(hass, dev_id)
if not entry.entry_id:
raise HomeAssistantError("unknown device id")

host = entry.data[CONF_DEVICES][dev_id].get(CONF_HOST)
device: TuyaDevice = hass.data[DOMAIN][entry.entry_id].tuya_devices[host]
device: TuyaDevice = hass.data[DOMAIN][entry.entry_id].tuya_devices[dev_id]
if not device.connected:
raise HomeAssistantError("not connected to device")
value = event.data[CONF_VALUE]
if isinstance(value, list):
data = {k: v for d in value for k, v in d.items()}
await device.set_dps(data)
else:
await device.set_dp(value, event.data[CONF_DP])

await device.set_dp(event.data[CONF_VALUE], event.data[CONF_DP])

def _device_discovered(device: dict):
"""Update address of device if it has changed."""
Expand Down Expand Up @@ -162,7 +157,11 @@ def _shutdown(event):
"""Clean up resources when shutting down."""
discovery.close()

hass.services.async_register(DOMAIN, SERVICE_RELOAD, _handle_reload)
hass.services.async_register(
DOMAIN,
SERVICE_RELOAD,
_handle_reload,
)

hass.services.async_register(
DOMAIN, SERVICE_SET_DP, _handle_set_dp, schema=SERVICE_SET_DP_SCHEMA
Expand Down Expand Up @@ -248,14 +247,15 @@ async def setup_entities(entry_devices: dict):
)

if node_id := config.get(CONF_NODE_ID):
# Setup sub device as gateway if there is no gateway exist.
# Setup sub device as gateway if no gateway not exist.
if host not in devices:
devices[host] = TuyaDevice(hass, entry, dev_id, True)

host = f"{host}_{node_id}"

devices[host] = TuyaDevice(hass, entry, dev_id)

# Unsub listener: callback to unsub
hass_localtuya = HassLocalTuyaData(tuya_api, devices, [])
hass.data[DOMAIN][entry.entry_id] = hass_localtuya

Expand All @@ -266,7 +266,6 @@ async def setup_entities(entry_devices: dict):
connect_to_devices = [
device.async_connect() for device in hass_localtuya.tuya_devices.values()
]
# Update listener: add to unsub_listeners
entry_update = entry.add_update_listener(update_listener)
hass_localtuya.unsub_listeners.append(entry_update)

Expand Down
4 changes: 2 additions & 2 deletions custom_components/localtuya/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def __init__(
hass: HomeAssistant,
config_entry: ConfigEntry,
dev_id: str,
fake_gateway=False,
gateway=False,
):
"""Initialize the cache."""
super().__init__()
Expand All @@ -168,7 +168,7 @@ def __init__(
self._interface = None
# For SubDevices
self._node_id: str = self._device_config.get(CONF_NODE_ID)
self._fake_gateway = fake_gateway
self._fake_gateway = gateway
self._gwateway: TuyaDevice = None
self._sub_devices: dict[str, TuyaDevice] = {}

Expand Down
2 changes: 1 addition & 1 deletion custom_components/localtuya/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ set_dp:
mode: box
value:
name: "Value"
description: "New value to set or list of dp: value, If value is list target dp will be ignored"
description: New value to set
required: true
example: "true"
selector:
Expand Down

0 comments on commit 73f5171

Please sign in to comment.