Skip to content

Commit

Permalink
Generic update service (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSimpleZ authored Jan 21, 2025
1 parent c03b8e4 commit e01c702
Show file tree
Hide file tree
Showing 4 changed files with 1,786 additions and 1,728 deletions.
40 changes: 33 additions & 7 deletions custom_components/connectlife/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
from .const import DOMAIN

ATTR_ACTION = "action"
ATTR_DATA = "data"
SERVICE_SET_ACTION = "set_action"
SERVICE_UPDATE = "update"

_LOGGER = logging.getLogger(__name__)

Expand All @@ -25,7 +27,7 @@ async def async_setup_services(hass: HomeAssistant) -> None:
"""Set up the services for the Fully Kiosk Browser integration."""

async def collect_coordinators(
device_ids: list[str],
device_ids: list[str],
) -> dict[str, ConnectLifeCoordinator]:
config_entries = dict[str, ConfigEntry]()
registry = dr.async_get(hass)
Expand All @@ -36,7 +38,7 @@ async def collect_coordinators(
for entry_id in device.config_entries:
entry = hass.config_entries.async_get_entry(entry_id)
if entry and entry.domain == DOMAIN:
for (domain, device_id) in device.identifiers:
for domain, device_id in device.identifiers:
if domain == DOMAIN:
_LOGGER.debug(f"device_id: {device_id}")
device_entries[device_id] = entry
Expand All @@ -57,14 +59,20 @@ async def collect_coordinators(
coordinators[device_id] = hass.data[DOMAIN][config_entry.entry_id]
return coordinators

async def async_set_action(call: ServiceCall) -> None:
"""Set action on device."""
coordinators = await collect_coordinators(call.data[ATTR_DEVICE_ID])
async def _async_update(devices: list[str], data: dict[str, any]) -> None:
"""Update properties on device."""
coordinators = await collect_coordinators(devices)
for device_id, coordinator in coordinators.items():
_LOGGER.debug("Setting Actions to %d on %s", call.data[ATTR_ACTION], device_id)
_LOGGER.debug(f"Updating {device_id} with data: {data}")
# TODO: Consider trigging a data update to avoid waiting for next poll to update state.
# Make sure to only do this once per coordinater.
await coordinator.async_update_device(device_id, {"Actions": call.data[ATTR_ACTION]}, {})
await coordinator.async_update_device(device_id, data, {})

async def async_set_action(call: ServiceCall) -> None:
"""Set action on device."""
await _async_update(
call.data[ATTR_DEVICE_ID], {"Actions": call.data[ATTR_ACTION]}
)

hass.services.async_register(
DOMAIN,
Expand All @@ -79,3 +87,21 @@ async def async_set_action(call: ServiceCall) -> None:
)
),
)

async def async_update(call: ServiceCall) -> None:
"""Action handler for updating properties on device."""
await _async_update(call.data[ATTR_DEVICE_ID], call.data[ATTR_DATA])

hass.services.async_register(
DOMAIN,
SERVICE_UPDATE,
async_update,
schema=vol.Schema(
vol.All(
{
vol.Required(ATTR_DEVICE_ID): cv.ensure_list,
vol.Required(ATTR_DATA): dict,
}
)
),
)
12 changes: 12 additions & 0 deletions custom_components/connectlife/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ set_action:
- "2"
- "3"
- "4"
update:
target:
device:
integration: connectlife
fields:
data:
required: true
example: |
Delay_start_status: 1
Delay_start_time: 4
selector:
object:
10 changes: 10 additions & 0 deletions custom_components/connectlife/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@
"description": "Action to set."
}
}
},
"update": {
"name": "Update device",
"description": "Updates all properties defined in the data field.",
"fields": {
"data": {
"name": "Data",
"description": "Properties that should be updated"
}
}
}
},
"entity": {
Expand Down
Loading

0 comments on commit e01c702

Please sign in to comment.