mirror of
https://github.com/home-assistant/core
synced 2024-09-06 10:29:55 +02: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."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from plugwise import SmileSwitches
|
||||
|
||||
from homeassistant.components.switch import (
|
||||
SwitchDeviceClass,
|
||||
SwitchEntity,
|
||||
@ -18,29 +22,46 @@ from .coordinator import PlugwiseDataUpdateCoordinator
|
||||
from .entity import PlugwiseEntity
|
||||
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",
|
||||
translation_key="dhw_cm_switch",
|
||||
icon="mdi:water-plus",
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
value_fn=lambda data: data["dhw_cm_switch"],
|
||||
),
|
||||
SwitchEntityDescription(
|
||||
PlugwiseSwitchEntityDescription(
|
||||
key="lock",
|
||||
translation_key="lock",
|
||||
icon="mdi:lock",
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
value_fn=lambda data: data["lock"],
|
||||
),
|
||||
SwitchEntityDescription(
|
||||
PlugwiseSwitchEntityDescription(
|
||||
key="relay",
|
||||
translation_key="relay",
|
||||
device_class=SwitchDeviceClass.SWITCH,
|
||||
value_fn=lambda data: data["relay"],
|
||||
),
|
||||
SwitchEntityDescription(
|
||||
PlugwiseSwitchEntityDescription(
|
||||
key="cooling_ena_switch",
|
||||
name="Cooling",
|
||||
icon="mdi:snowflake-thermometer",
|
||||
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]
|
||||
entities: list[PlugwiseSwitchEntity] = []
|
||||
for device_id, device in coordinator.data.devices.items():
|
||||
if not (switches := device.get("switches")):
|
||||
continue
|
||||
for description in SWITCHES:
|
||||
if "switches" not in device or description.key not in device["switches"]:
|
||||
if description.key not in switches:
|
||||
continue
|
||||
entities.append(PlugwiseSwitchEntity(coordinator, device_id, description))
|
||||
async_add_entities(entities)
|
||||
@ -64,11 +87,13 @@ async def async_setup_entry(
|
||||
class PlugwiseSwitchEntity(PlugwiseEntity, SwitchEntity):
|
||||
"""Representation of a Plugwise plug."""
|
||||
|
||||
entity_description: PlugwiseSwitchEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: PlugwiseDataUpdateCoordinator,
|
||||
device_id: str,
|
||||
description: SwitchEntityDescription,
|
||||
description: PlugwiseSwitchEntityDescription,
|
||||
) -> None:
|
||||
"""Set up the Plugwise API."""
|
||||
super().__init__(coordinator, device_id)
|
||||
@ -76,9 +101,9 @@ class PlugwiseSwitchEntity(PlugwiseEntity, SwitchEntity):
|
||||
self._attr_unique_id = f"{device_id}-{description.key}"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
def is_on(self) -> bool:
|
||||
"""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
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
|
Loading…
Reference in New Issue
Block a user