core/homeassistant/components/enocean/switch.py

144 lines
4.8 KiB
Python
Raw Normal View History

"""Support for EnOcean switches."""
from __future__ import annotations
2022-08-20 05:52:55 +00:00
from typing import Any
from enocean.utils import combine_hex
2016-08-25 04:35:09 +00:00
import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
from homeassistant.const import CONF_ID, CONF_NAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
2016-08-25 04:35:09 +00:00
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import DOMAIN, LOGGER
Refactor Enocean part 1 (#35927) * First step of an EnOcean integration refactoring, including code reorganisation and support of a setup config flow * Moved title to root of strings file * Fixed pre-commit checks failures * Fixed linter errors * Updated formatted string format in logs * Removed leftover comment * Multiple changes after PR change requests. Using an import flow for yaml config, removed unnecessary logs, added proper unload in __init__ and EnOceanDongle Replaced config state machine by several flows. Serial port validity check done in the EnOceanDongle class asynchronously, removed unique ID from config flow Multiple cosmetic changes * Multiple changes after PR change requests * Added variable to store default value, as setdefault was caught returning None when the empty dict literal was passed as an argument * Literal used directly * Added tests for EnOcean config flows, changed static methods to bundle methods for bundle * Updated variable name * Added missing mock to test, replaced repeated magic strings by constants * Changed imports to avoid an unused import warning from pylint on DOMAIN * Adding pylint exception for unused import * Added proper propagation of setup and unload to platforms, removed dead code, some syntax changes * Removed setup_entry forwarding as the entities can only be configured using yaml * Removed forwarding of unload * Enabled code coverage for config flow only * Clean up coveragerc Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2020-07-09 00:46:38 +00:00
from .device import EnOceanEntity
2019-07-31 19:25:30 +00:00
CONF_CHANNEL = "channel"
DEFAULT_NAME = "EnOcean Switch"
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_ID): vol.All(cv.ensure_list, [vol.Coerce(int)]),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_CHANNEL, default=0): cv.positive_int,
}
)
def generate_unique_id(dev_id: list[int], channel: int) -> str:
"""Generate a valid unique id."""
return f"{combine_hex(dev_id)}-{channel}"
def _migrate_to_new_unique_id(hass: HomeAssistant, dev_id, channel) -> None:
"""Migrate old unique ids to new unique ids."""
old_unique_id = f"{combine_hex(dev_id)}"
ent_reg = entity_registry.async_get(hass)
entity_id = ent_reg.async_get_entity_id(Platform.SWITCH, DOMAIN, old_unique_id)
if entity_id is not None:
new_unique_id = generate_unique_id(dev_id, channel)
try:
ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id)
except ValueError:
LOGGER.warning(
"Skip migration of id [%s] to [%s] because it already exists",
old_unique_id,
new_unique_id,
)
else:
LOGGER.debug(
"Migrating unique_id from [%s] to [%s]",
old_unique_id,
new_unique_id,
)
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the EnOcean switch platform."""
channel = config.get(CONF_CHANNEL)
dev_id = config.get(CONF_ID)
dev_name = config.get(CONF_NAME)
_migrate_to_new_unique_id(hass, dev_id, channel)
async_add_entities([EnOceanSwitch(dev_id, dev_name, channel)])
class EnOceanSwitch(EnOceanEntity, SwitchEntity):
"""Representation of an EnOcean switch device."""
def __init__(self, dev_id, dev_name, channel):
"""Initialize the EnOcean switch device."""
super().__init__(dev_id, dev_name)
self._light = None
self._on_state = False
self._on_state2 = False
self.channel = channel
self._attr_unique_id = generate_unique_id(dev_id, channel)
@property
def is_on(self):
"""Return whether the switch is on or off."""
return self._on_state
@property
def name(self):
"""Return the device name."""
return self.dev_name
2022-08-20 05:52:55 +00:00
def turn_on(self, **kwargs: Any) -> None:
"""Turn on the switch."""
2019-07-31 19:25:30 +00:00
optional = [0x03]
optional.extend(self.dev_id)
2019-07-31 19:25:30 +00:00
optional.extend([0xFF, 0x00])
self.send_command(
data=[0xD2, 0x01, self.channel & 0xFF, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00],
optional=optional,
packet_type=0x01,
)
self._on_state = True
2022-08-20 05:52:55 +00:00
def turn_off(self, **kwargs: Any) -> None:
"""Turn off the switch."""
2019-07-31 19:25:30 +00:00
optional = [0x03]
optional.extend(self.dev_id)
2019-07-31 19:25:30 +00:00
optional.extend([0xFF, 0x00])
self.send_command(
data=[0xD2, 0x01, self.channel & 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
optional=optional,
packet_type=0x01,
)
self._on_state = False
def value_changed(self, packet):
"""Update the internal state of the switch."""
2019-07-31 19:25:30 +00:00
if packet.data[0] == 0xA5:
# power meter telegram, turn on if > 10 watts
packet.parse_eep(0x12, 0x01)
2019-07-31 19:25:30 +00:00
if packet.parsed["DT"]["raw_value"] == 1:
raw_val = packet.parsed["MR"]["raw_value"]
divisor = packet.parsed["DIV"]["raw_value"]
2022-02-05 13:19:37 +00:00
watts = raw_val / (10**divisor)
if watts > 1:
self._on_state = True
self.schedule_update_ha_state()
2019-07-31 19:25:30 +00:00
elif packet.data[0] == 0xD2:
# actuator status telegram
packet.parse_eep(0x01, 0x01)
2019-07-31 19:25:30 +00:00
if packet.parsed["CMD"]["raw_value"] == 4:
channel = packet.parsed["IO"]["raw_value"]
output = packet.parsed["OV"]["raw_value"]
if channel == self.channel:
self._on_state = output > 0
self.schedule_update_ha_state()