mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Plugwise add value_fn for switch (#93345)
* Plugwise add value_fn for switch * Skip none as suggested
This commit is contained in:
parent
71466e5012
commit
af3bbdee82
@ -1,8 +1,12 @@
|
|||||||
"""Plugwise Switch component for HomeAssistant."""
|
"""Plugwise Switch component for HomeAssistant."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
from plugwise import SmileSwitches
|
||||||
|
|
||||||
from homeassistant.components.switch import (
|
from homeassistant.components.switch import (
|
||||||
SwitchDeviceClass,
|
SwitchDeviceClass,
|
||||||
SwitchEntity,
|
SwitchEntity,
|
||||||
@ -18,29 +22,46 @@ from .coordinator import PlugwiseDataUpdateCoordinator
|
|||||||
from .entity import PlugwiseEntity
|
from .entity import PlugwiseEntity
|
||||||
from .util import plugwise_command
|
from .util import plugwise_command
|
||||||
|
|
||||||
SWITCHES: tuple[SwitchEntityDescription, ...] = (
|
|
||||||
SwitchEntityDescription(
|
@dataclass
|
||||||
|
class PlugwiseSwitchBaseMixin:
|
||||||
|
"""Mixin for required Plugwise switch description keys."""
|
||||||
|
|
||||||
|
value_fn: Callable[[SmileSwitches], bool]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class PlugwiseSwitchEntityDescription(SwitchEntityDescription, PlugwiseSwitchBaseMixin):
|
||||||
|
"""Describes Plugwise switch entity."""
|
||||||
|
|
||||||
|
|
||||||
|
SWITCHES: tuple[PlugwiseSwitchEntityDescription, ...] = (
|
||||||
|
PlugwiseSwitchEntityDescription(
|
||||||
key="dhw_cm_switch",
|
key="dhw_cm_switch",
|
||||||
translation_key="dhw_cm_switch",
|
translation_key="dhw_cm_switch",
|
||||||
icon="mdi:water-plus",
|
icon="mdi:water-plus",
|
||||||
entity_category=EntityCategory.CONFIG,
|
entity_category=EntityCategory.CONFIG,
|
||||||
|
value_fn=lambda data: data["dhw_cm_switch"],
|
||||||
),
|
),
|
||||||
SwitchEntityDescription(
|
PlugwiseSwitchEntityDescription(
|
||||||
key="lock",
|
key="lock",
|
||||||
translation_key="lock",
|
translation_key="lock",
|
||||||
icon="mdi:lock",
|
icon="mdi:lock",
|
||||||
entity_category=EntityCategory.CONFIG,
|
entity_category=EntityCategory.CONFIG,
|
||||||
|
value_fn=lambda data: data["lock"],
|
||||||
),
|
),
|
||||||
SwitchEntityDescription(
|
PlugwiseSwitchEntityDescription(
|
||||||
key="relay",
|
key="relay",
|
||||||
translation_key="relay",
|
translation_key="relay",
|
||||||
device_class=SwitchDeviceClass.SWITCH,
|
device_class=SwitchDeviceClass.SWITCH,
|
||||||
|
value_fn=lambda data: data["relay"],
|
||||||
),
|
),
|
||||||
SwitchEntityDescription(
|
PlugwiseSwitchEntityDescription(
|
||||||
key="cooling_ena_switch",
|
key="cooling_ena_switch",
|
||||||
name="Cooling",
|
name="Cooling",
|
||||||
icon="mdi:snowflake-thermometer",
|
icon="mdi:snowflake-thermometer",
|
||||||
entity_category=EntityCategory.CONFIG,
|
entity_category=EntityCategory.CONFIG,
|
||||||
|
value_fn=lambda data: data["cooling_ena_switch"],
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -54,8 +75,10 @@ async def async_setup_entry(
|
|||||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||||
entities: list[PlugwiseSwitchEntity] = []
|
entities: list[PlugwiseSwitchEntity] = []
|
||||||
for device_id, device in coordinator.data.devices.items():
|
for device_id, device in coordinator.data.devices.items():
|
||||||
|
if not (switches := device.get("switches")):
|
||||||
|
continue
|
||||||
for description in SWITCHES:
|
for description in SWITCHES:
|
||||||
if "switches" not in device or description.key not in device["switches"]:
|
if description.key not in switches:
|
||||||
continue
|
continue
|
||||||
entities.append(PlugwiseSwitchEntity(coordinator, device_id, description))
|
entities.append(PlugwiseSwitchEntity(coordinator, device_id, description))
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
@ -64,11 +87,13 @@ async def async_setup_entry(
|
|||||||
class PlugwiseSwitchEntity(PlugwiseEntity, SwitchEntity):
|
class PlugwiseSwitchEntity(PlugwiseEntity, SwitchEntity):
|
||||||
"""Representation of a Plugwise plug."""
|
"""Representation of a Plugwise plug."""
|
||||||
|
|
||||||
|
entity_description: PlugwiseSwitchEntityDescription
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: PlugwiseDataUpdateCoordinator,
|
coordinator: PlugwiseDataUpdateCoordinator,
|
||||||
device_id: str,
|
device_id: str,
|
||||||
description: SwitchEntityDescription,
|
description: PlugwiseSwitchEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Plugwise API."""
|
"""Set up the Plugwise API."""
|
||||||
super().__init__(coordinator, device_id)
|
super().__init__(coordinator, device_id)
|
||||||
@ -76,9 +101,9 @@ class PlugwiseSwitchEntity(PlugwiseEntity, SwitchEntity):
|
|||||||
self._attr_unique_id = f"{device_id}-{description.key}"
|
self._attr_unique_id = f"{device_id}-{description.key}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool | None:
|
def is_on(self) -> bool:
|
||||||
"""Return True if entity is on."""
|
"""Return True if entity is on."""
|
||||||
return self.device["switches"].get(self.entity_description.key)
|
return self.entity_description.value_fn(self.device["switches"])
|
||||||
|
|
||||||
@plugwise_command
|
@plugwise_command
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user