|
| 1 | +"""Plugwise USB Event component for HomeAssistant.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from dataclasses import dataclass |
| 6 | +from datetime import timedelta |
| 7 | +import logging |
| 8 | + |
| 9 | +from plugwise_usb.api import NodeEvent, NodeFeature |
| 10 | + |
| 11 | +from homeassistant.components.event import ( |
| 12 | + EventDeviceClass, |
| 13 | + EventEntity, |
| 14 | + EventEntityDescription, |
| 15 | +) |
| 16 | +from homeassistant.const import EntityCategory, Platform, UnitOfTime |
| 17 | +from homeassistant.core import HomeAssistant, callback |
| 18 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 19 | + |
| 20 | +from .const import NODES, STICK, UNSUB_NODE_LOADED |
| 21 | +from .coordinator import PlugwiseUSBConfigEntry, PlugwiseUSBDataUpdateCoordinator |
| 22 | +from .entity import PlugwiseUSBEntity, PlugwiseUSBEntityDescription |
| 23 | + |
| 24 | +_LOGGER = logging.getLogger(__name__) |
| 25 | +PARALLEL_UPDATES = 2 |
| 26 | +SCAN_INTERVAL = timedelta(seconds=30) |
| 27 | + |
| 28 | + |
| 29 | +@dataclass(kw_only=True) |
| 30 | +class PlugwiseEventEntityDescription( |
| 31 | + PlugwiseUSBEntityDescription, EventEntityDescription |
| 32 | +): |
| 33 | + """Describes Plugwise Event entity.""" |
| 34 | + |
| 35 | + api_attribute: str = "" |
| 36 | + |
| 37 | + |
| 38 | +EVENT_TYPES: tuple[PlugwiseEventEntityDescription, ...] = ( |
| 39 | + PlugwiseEventEntityDescription( |
| 40 | + key="button_press_i_group_1", |
| 41 | + translation_key="button_press_i_group_1", |
| 42 | + node_feature=NodeFeature.SWITCH, |
| 43 | + device_class=EventDeviceClass.BUTTON, |
| 44 | + api_attribute="state", |
| 45 | + event_types=["single_press"], |
| 46 | + ), |
| 47 | + PlugwiseEventEntityDescription( |
| 48 | + key="button_press_o_group_1", |
| 49 | + translation_key="button_press_o_group_1", |
| 50 | + node_feature=NodeFeature.SWITCH, |
| 51 | + device_class=EventDeviceClass.BUTTON, |
| 52 | + api_attribute="state", |
| 53 | + event_types=["single_press"], |
| 54 | + ), |
| 55 | + PlugwiseEventEntityDescription( |
| 56 | + key="button_press_i_group_2", |
| 57 | + translation_key="button_press_i_group_2", |
| 58 | + node_feature=NodeFeature.SWITCH, |
| 59 | + device_class=EventDeviceClass.BUTTON, |
| 60 | + api_attribute="state", |
| 61 | + event_types=["single_press"], |
| 62 | + ), |
| 63 | + PlugwiseEventEntityDescription( |
| 64 | + key="button_press_o_group_2", |
| 65 | + translation_key="button_press_o_group_2", |
| 66 | + node_feature=NodeFeature.SWITCH, |
| 67 | + device_class=EventDeviceClass.BUTTON, |
| 68 | + api_attribute="state", |
| 69 | + event_types=["single_press"], |
| 70 | + ), |
| 71 | +) |
| 72 | + |
| 73 | + |
| 74 | +async def async_setup_entry( |
| 75 | + _hass: HomeAssistant, |
| 76 | + config_entry: PlugwiseUSBConfigEntry, |
| 77 | + async_add_entities: AddEntitiesCallback, |
| 78 | +) -> None: |
| 79 | + """Set up the USB Event from a config entry.""" |
| 80 | + async def async_add_event(node_event: NodeEvent, mac: str) -> None: |
| 81 | + """Initialize DUC for event.""" |
| 82 | + if node_event != NodeEvent.LOADED: |
| 83 | + return |
| 84 | + entities: list[PlugwiseUSBEntity] = [] |
| 85 | + if (node_duc := config_entry.runtime_data[NODES].get(mac)) is not None: |
| 86 | + _LOGGER.debug("Add event entities for %s | duc=%s", mac, node_duc.name) |
| 87 | + entities.extend( |
| 88 | + [ |
| 89 | + PlugwiseUSBEventEntity(node_duc, entity_description) |
| 90 | + for entity_description in EVENT_TYPES |
| 91 | + if entity_description.node_feature in node_duc.node.features |
| 92 | + ] |
| 93 | + ) |
| 94 | + if entities: |
| 95 | + async_add_entities(entities, update_before_add=True) |
| 96 | + |
| 97 | + api_stick = config_entry.runtime_data[STICK] |
| 98 | + |
| 99 | + # Listen for loaded nodes |
| 100 | + config_entry.runtime_data[Platform.EVENT] = {} |
| 101 | + config_entry.runtime_data[Platform.EVENT][UNSUB_NODE_LOADED] = ( |
| 102 | + api_stick.subscribe_to_node_events( |
| 103 | + async_add_event, |
| 104 | + (NodeEvent.LOADED,), |
| 105 | + ) |
| 106 | + ) |
| 107 | + |
| 108 | + # load any current nodes |
| 109 | + for mac, node in api_stick.nodes.items(): |
| 110 | + if node.is_loaded: |
| 111 | + await async_add_event(NodeEvent.LOADED, mac) |
| 112 | + |
| 113 | + |
| 114 | +async def async_unload_entry( |
| 115 | + config_entry: PlugwiseUSBConfigEntry, |
| 116 | +) -> None: |
| 117 | + """Unload a config entry.""" |
| 118 | + config_entry.runtime_data[Platform.EVENT][UNSUB_NODE_LOADED]() |
| 119 | + |
| 120 | + |
| 121 | +class PlugwiseUSBEventEntity(PlugwiseUSBEntity, EventEntity): |
| 122 | + """Representation of a Plugwise USB Data Update Coordinator Event entity.""" |
| 123 | + |
| 124 | + def __init__( |
| 125 | + self, |
| 126 | + node_duc: PlugwiseUSBDataUpdateCoordinator, |
| 127 | + entity_description: PlugwiseEventEntityDescription, |
| 128 | + ) -> None: |
| 129 | + """Initialize a event entity.""" |
| 130 | + super().__init__(node_duc, entity_description) |
| 131 | + |
| 132 | + @callback |
| 133 | + def _handle_coordinator_update(self) -> None: |
| 134 | + """Handle updated data from the coordinator.""" |
| 135 | + data = self.coordinator.data.get(self.entity_description.node_feature, None) |
| 136 | + if data is None: |
| 137 | + _LOGGER.debug( |
| 138 | + "No %s event data for %s", |
| 139 | + self.entity_description.node_feature, |
| 140 | + self._node_info.mac, |
| 141 | + ) |
| 142 | + return |
| 143 | + # SWITCH logic |
| 144 | + state_value = getattr(data, "state" ) |
| 145 | + group_value = getattr(data, "group" ) |
| 146 | + match self.entity_description.key: |
| 147 | + case "button_press_i_group_1": |
| 148 | + if state_value is True and group_value == 1: |
| 149 | + self._trigger_event(self.entity_description.event_types[0]) |
| 150 | + self.async_write_ha_state() |
| 151 | + return |
| 152 | + case "button_press_o_group_1": |
| 153 | + if state_value is False and group_value == 1: |
| 154 | + self._trigger_event(self.entity_description.event_types[0]) |
| 155 | + self.async_write_ha_state() |
| 156 | + return |
| 157 | + case "button_press_i_group_2": |
| 158 | + if state_value is True and group_value == 2: |
| 159 | + self._trigger_event(self.entity_description.event_types[0]) |
| 160 | + self.async_write_ha_state() |
| 161 | + return |
| 162 | + case "button_press_o_group_2": |
| 163 | + if state_value is False and group_value == 2: |
| 164 | + self._trigger_event(self.entity_description.event_types[0]) |
| 165 | + self.async_write_ha_state() |
| 166 | + return |
| 167 | + |
0 commit comments