1 """Platform for NASweb output."""
3 from __future__
import annotations
9 from webio_api
import Output
as NASwebOutput
18 BaseCoordinatorEntity,
19 BaseDataUpdateCoordinatorProtocol,
22 from .
import NASwebConfigEntry
23 from .const
import DOMAIN, STATUS_UPDATE_MAX_TIME_INTERVAL
24 from .coordinator
import NASwebCoordinator
26 OUTPUT_TRANSLATION_KEY =
"switch_output"
28 _LOGGER = logging.getLogger(__name__)
31 def _get_output(coordinator: NASwebCoordinator, index: int) -> NASwebOutput |
None:
32 for out
in coordinator.webio_api.outputs:
33 if out.index == index:
40 config: NASwebConfigEntry,
41 async_add_entities: AddEntitiesCallback,
42 discovery_info: DiscoveryInfoType |
None =
None,
44 """Set up switch platform."""
45 coordinator = config.runtime_data
46 current_outputs: set[int] = set()
49 def _check_entities() -> None:
50 received_outputs = {out.index
for out
in coordinator.webio_api.outputs}
51 added = {i
for i
in received_outputs
if i
not in current_outputs}
52 removed = {i
for i
in current_outputs
if i
not in received_outputs}
53 entities_to_add: list[RelaySwitch] = []
56 if not isinstance(webio_output, NASwebOutput):
57 _LOGGER.error(
"Cannot create RelaySwitch entity without NASwebOutput")
60 entities_to_add.append(new_output)
61 current_outputs.add(index)
63 entity_registry = er.async_get(hass)
65 unique_id = f
"{DOMAIN}.{config.unique_id}.relay_switch.{index}"
66 if entity_id := entity_registry.async_get_entity_id(
67 DOMAIN_SWITCH, DOMAIN, unique_id
69 entity_registry.async_remove(entity_id)
70 current_outputs.remove(index)
72 _LOGGER.warning(
"Failed to remove old output: no entity_id")
74 coordinator.async_add_listener(_check_entities)
79 """Entity representing NASweb Output."""
83 coordinator: BaseDataUpdateCoordinatorProtocol,
84 nasweb_output: NASwebOutput,
86 """Initialize RelaySwitch."""
94 f
"{DOMAIN}.{self._output.webio_serial}.relay_switch.{self._output.index}"
97 identifiers={(DOMAIN, self.
_output_output.webio_serial)},
101 """When entity is added to hass."""
107 """Handle updated data from the coordinator."""
110 self.coordinator.last_update
is None
111 or time.time() - self.
_output_output.last_update >= STATUS_UPDATE_MAX_TIME_INTERVAL
116 self.
_output_output.available
if self.
_output_output.available
is not None else False
118 self.async_write_ha_state()
121 """Update the entity.
123 Only used by the generic entity update service.
124 Scheduling updates is not necessary, the coordinator takes care of updates via push notifications.
128 """Turn On RelaySwitch."""
129 await self._output.turn_on()
132 """Turn Off RelaySwitch."""
133 await self.
_output_output.turn_off()
None async_added_to_hass(self)
None __init__(self, BaseDataUpdateCoordinatorProtocol coordinator, NASwebOutput nasweb_output)
None async_turn_on(self, **Any kwargs)
None _handle_coordinator_update(self)
_attr_translation_placeholders
None async_turn_off(self, **Any kwargs)
None async_setup_entry(HomeAssistant hass, NASwebConfigEntry config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
NASwebOutput|None _get_output(NASwebCoordinator coordinator, int index)