switch continued

This commit is contained in:
Taraman17 2025-02-02 21:07:31 +00:00
parent 92d9560ac2
commit a0a653e89b
2 changed files with 35 additions and 29 deletions

View File

@ -7,6 +7,14 @@
"window_position": { "window_position": {
"default": "mdi:window-closed" "default": "mdi:window-closed"
} }
},
"switch": {
"watchdog_on_off": {
"default": "mdi:dog"
},
"manual_operation": {
"default": "mdi:hand-back-left"
}
} }
} }
} }

View File

@ -1,10 +1,10 @@
"""The homee switch platform.""" """The homee switch platform."""
from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
import logging
from typing import Any from typing import Any
from pyHomee.const import AttributeType from pyHomee.const import AttributeType, NodeProfile
from pyHomee.model import HomeeAttribute from pyHomee.model import HomeeAttribute
from homeassistant.components.switch import ( from homeassistant.components.switch import (
@ -20,31 +20,30 @@ from . import HomeeConfigEntry
from .const import CLIMATE_PROFILES, LIGHT_PROFILES from .const import CLIMATE_PROFILES, LIGHT_PROFILES
from .entity import HomeeEntity from .entity import HomeeEntity
_LOGGER = logging.getLogger(__name__)
DESCRIPTIVE_ATTRIBUTES = [ def get_device_class(
AttributeType.AUTOMATIC_MODE_IMPULSE, attribute: HomeeAttribute, config_entry: HomeeConfigEntry
AttributeType.BRIEFLY_OPEN_IMPULSE, ) -> SwitchDeviceClass:
AttributeType.EXTERNAL_BINARY_INPUT, """Check device class of Switch according to node profile."""
AttributeType.IDENTIFICATION_MODE, node = config_entry.runtime_data.get_node_by_id(attribute.node_id)
AttributeType.LIGHT_IMPULSE, if node.profile in [
AttributeType.MANUAL_OPERATION, NodeProfile.ON_OFF_PLUG,
AttributeType.MOTOR_ROTATION, NodeProfile.METERING_PLUG,
AttributeType.OPEN_PARTIAL_IMPULSE, NodeProfile.DOUBLE_ON_OFF_PLUG,
AttributeType.PERMANENTLY_OPEN_IMPULSE, NodeProfile.IMPULSE_PLUG,
AttributeType.RESET_METER, ]:
AttributeType.RESTORE_LAST_KNOWN_STATE, return SwitchDeviceClass.OUTLET
AttributeType.SWITCH_TYPE,
AttributeType.VENTILATE_IMPULSE, return SwitchDeviceClass.SWITCH
AttributeType.WATCHDOG_ON_OFF,
]
@dataclass(frozen=True, kw_only=True) @dataclass(frozen=True, kw_only=True)
class HomeeSwitchEntityDescription(SwitchEntityDescription): class HomeeSwitchEntityDescription(SwitchEntityDescription):
"""A class that describes Homee switch entity.""" """A class that describes Homee switch entity."""
device_class: SwitchDeviceClass = SwitchDeviceClass.SWITCH device_class_fn: Callable[[HomeeAttribute, HomeeConfigEntry], SwitchDeviceClass] = (
lambda attribute, entry: SwitchDeviceClass.SWITCH
)
SWITCH_DESCRIPTIONS: dict[AttributeType, HomeeSwitchEntityDescription] = { SWITCH_DESCRIPTIONS: dict[AttributeType, HomeeSwitchEntityDescription] = {
@ -71,7 +70,9 @@ SWITCH_DESCRIPTIONS: dict[AttributeType, HomeeSwitchEntityDescription] = {
AttributeType.OPEN_PARTIAL_IMPULSE: HomeeSwitchEntityDescription( AttributeType.OPEN_PARTIAL_IMPULSE: HomeeSwitchEntityDescription(
key="open_partial_impulse" key="open_partial_impulse"
), ),
AttributeType.ON_OFF: HomeeSwitchEntityDescription(key="on_off"), AttributeType.ON_OFF: HomeeSwitchEntityDescription(
key="on_off", device_class_fn=get_device_class
),
AttributeType.PERMANENTLY_OPEN_IMPULSE: HomeeSwitchEntityDescription( AttributeType.PERMANENTLY_OPEN_IMPULSE: HomeeSwitchEntityDescription(
key="permanently_open_impulse" key="permanently_open_impulse"
), ),
@ -122,6 +123,8 @@ async def async_setup_entry(
class HomeeSwitch(HomeeEntity, SwitchEntity): class HomeeSwitch(HomeeEntity, SwitchEntity):
"""Representation of a homee switch.""" """Representation of a homee switch."""
entity_description: HomeeSwitchEntityDescription
def __init__( def __init__(
self, self,
attribute: HomeeAttribute, attribute: HomeeAttribute,
@ -138,14 +141,9 @@ class HomeeSwitch(HomeeEntity, SwitchEntity):
self._attr_translation_placeholders = {"instance": str(attribute.instance)} self._attr_translation_placeholders = {"instance": str(attribute.instance)}
@property @property
def icon(self) -> str | None: def device_class(self) -> SwitchDeviceClass:
"""Return icon if different from main feature.""" """Return the device class of the switch."""
if self._attribute.type == AttributeType.WATCHDOG_ON_OFF: return self.entity_description.device_class_fn(self._attribute, self._entry)
return "mdi:dog"
if self._attribute.type == AttributeType.MANUAL_OPERATION:
return "mdi:hand-back-left"
return None
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on.""" """Turn the entity on."""