1 """Support for EnOcean switches."""
3 from __future__
import annotations
7 from enocean.utils
import combine_hex
8 import voluptuous
as vol
11 PLATFORM_SCHEMA
as SWITCH_PLATFORM_SCHEMA,
20 from .const
import DOMAIN, LOGGER
21 from .entity
import EnOceanEntity
23 CONF_CHANNEL =
"channel"
24 DEFAULT_NAME =
"EnOcean Switch"
26 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
28 vol.Required(CONF_ID): vol.All(cv.ensure_list, [vol.Coerce(int)]),
29 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
30 vol.Optional(CONF_CHANNEL, default=0): cv.positive_int,
36 """Generate a valid unique id."""
37 return f
"{combine_hex(dev_id)}-{channel}"
41 """Migrate old unique ids to new unique ids."""
42 old_unique_id = f
"{combine_hex(dev_id)}"
44 ent_reg = er.async_get(hass)
45 entity_id = ent_reg.async_get_entity_id(Platform.SWITCH, DOMAIN, old_unique_id)
47 if entity_id
is not None:
50 ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id)
53 "Skip migration of id [%s] to [%s] because it already exists",
59 "Migrating unique_id from [%s] to [%s]",
68 async_add_entities: AddEntitiesCallback,
69 discovery_info: DiscoveryInfoType |
None =
None,
71 """Set up the EnOcean switch platform."""
72 channel: int = config[CONF_CHANNEL]
73 dev_id: list[int] = config[CONF_ID]
74 dev_name: str = config[CONF_NAME]
81 """Representation of an EnOcean switch device."""
85 def __init__(self, dev_id: list[int], dev_name: str, channel: int) ->
None:
86 """Initialize the EnOcean switch device."""
94 """Turn on the switch."""
96 optional.extend(self.
dev_iddev_id)
97 optional.extend([0xFF, 0x00])
99 data=[0xD2, 0x01, self.
channelchannel & 0xFF, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00],
106 """Turn off the switch."""
108 optional.extend(self.
dev_iddev_id)
109 optional.extend([0xFF, 0x00])
111 data=[0xD2, 0x01, self.
channelchannel & 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
118 """Update the internal state of the switch."""
119 if packet.data[0] == 0xA5:
121 packet.parse_eep(0x12, 0x01)
122 if packet.parsed[
"DT"][
"raw_value"] == 1:
123 raw_val = packet.parsed[
"MR"][
"raw_value"]
124 divisor = packet.parsed[
"DIV"][
"raw_value"]
125 watts = raw_val / (10**divisor)
129 elif packet.data[0] == 0xD2:
131 packet.parse_eep(0x01, 0x01)
132 if packet.parsed[
"CMD"][
"raw_value"] == 4:
133 channel = packet.parsed[
"IO"][
"raw_value"]
134 output = packet.parsed[
"OV"][
"raw_value"]
135 if channel == self.
channelchannel:
def send_command(self, data, optional, packet_type)
None turn_off(self, **Any kwargs)
def value_changed(self, packet)
None turn_on(self, **Any kwargs)
None __init__(self, list[int] dev_id, str dev_name, int channel)
None schedule_update_ha_state(self, bool force_refresh=False)
None _migrate_to_new_unique_id(HomeAssistant hass, dev_id, channel)
str generate_unique_id(list[int] dev_id, int channel)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)