Skip to content

Commit

Permalink
Merge pull request #1 from rhys-e2/set_dp_service
Browse files Browse the repository at this point in the history
Set dp service
  • Loading branch information
rhys-e2 authored Dec 6, 2024
2 parents 2d205a4 + e14a4e4 commit a4f3986
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions custom_components/tuya_local/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.entity_registry import async_migrate_entries
from homeassistant.util import slugify
import homeassistant.helpers.config_validation as cv
from homeassistant.exceptions import HomeAssistantError
import voluptuous as vol


from .const import (
CONF_DEVICE_ID,
Expand All @@ -30,6 +34,18 @@
_LOGGER = logging.getLogger(__name__)
NOT_FOUND = "Configuration file for %s not found"

CONF_DP = "dp"
CONF_VALUE = "value"

SERVICE_SET_DP = "set_dp"
SERVICE_SET_DP_SCHEMA = vol.Schema(
{
vol.Required(CONF_DEVICE_ID): cv.string,
vol.Required(CONF_DP): int,
vol.Required(CONF_VALUE): object,
}
)


def replace_unique_ids(entity_entry, device_id, conf_file, replacements):
"""Update the unique id of an entry based on a table of replacements."""
Expand Down Expand Up @@ -645,6 +661,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):

entry.add_update_listener(async_update_entry)

async def _handle_set_dp(event):
"""Handle set_dp service call."""
dev_id = event.data[CONF_DEVICE_ID]
if dev_id not in hass.data[DOMAIN]:
raise HomeAssistantError("unknown device id")

device = hass.data[DOMAIN][dev_id]["device"]

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

hass.services.async_register(
DOMAIN, SERVICE_SET_DP, _handle_set_dp, schema=SERVICE_SET_DP_SCHEMA
)

return True


Expand Down

0 comments on commit a4f3986

Please sign in to comment.