Use SwitchEntityDescription in kostal plenticore (#78841)

This commit is contained in:
epenet 2022-09-21 11:30:17 +02:00 committed by GitHub
parent e265848b63
commit 67f7c17d34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,11 +2,12 @@
from __future__ import annotations from __future__ import annotations
from abc import ABC from abc import ABC
from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import Any, NamedTuple from typing import Any
from homeassistant.components.switch import SwitchEntity from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo, EntityCategory from homeassistant.helpers.entity import DeviceInfo, EntityCategory
@ -19,12 +20,11 @@ from .helper import SettingDataUpdateCoordinator
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
class SwitchData(NamedTuple): @dataclass
"""Representation of a SelectData tuple.""" class PlenticoreRequiredKeysMixin:
"""A class that describes required properties for plenticore switch entities."""
module_id: str module_id: str
data_id: str
name: str
is_on: str is_on: str
on_value: str on_value: str
on_label: str on_label: str
@ -32,26 +32,23 @@ class SwitchData(NamedTuple):
off_label: str off_label: str
# Defines all entities for switches. @dataclass
# class PlenticoreSwitchEntityDescription(
# Each entry is defined with a tuple of these values: SwitchEntityDescription, PlenticoreRequiredKeysMixin
# - module id (str) ):
# - process data id (str) """A class that describes plenticore switch entities."""
# - entity name suffix (str)
# - on Value (str)
# - on Label (str)
# - off Value (str)
# - off Label (str)
SWITCH_SETTINGS_DATA = [ SWITCH_SETTINGS_DATA = [
SwitchData( PlenticoreSwitchEntityDescription(
"devices:local", module_id="devices:local",
"Battery:Strategy", key="Battery:Strategy",
"Battery Strategy", name="Battery Strategy",
"1", is_on="1",
"1", on_value="1",
"Automatic", on_label="Automatic",
"2", off_value="2",
"Automatic economical", off_label="Automatic economical",
), ),
] ]
@ -73,13 +70,13 @@ async def async_setup_entry(
plenticore, plenticore,
) )
for switch in SWITCH_SETTINGS_DATA: for switch in SWITCH_SETTINGS_DATA:
if switch.module_id not in available_settings_data or switch.data_id not in ( if switch.module_id not in available_settings_data or switch.key not in (
setting.id for setting in available_settings_data[switch.module_id] setting.id for setting in available_settings_data[switch.module_id]
): ):
_LOGGER.debug( _LOGGER.debug(
"Skipping non existing setting data %s/%s", "Skipping non existing setting data %s/%s",
switch.module_id, switch.module_id,
switch.data_id, switch.key,
) )
continue continue
@ -89,7 +86,7 @@ async def async_setup_entry(
entry.entry_id, entry.entry_id,
entry.title, entry.title,
switch.module_id, switch.module_id,
switch.data_id, switch.key,
switch.name, switch.name,
switch.is_on, switch.is_on,
switch.on_value, switch.on_value,
@ -98,7 +95,7 @@ async def async_setup_entry(
switch.off_label, switch.off_label,
plenticore.device_info, plenticore.device_info,
f"{entry.title} {switch.name}", f"{entry.title} {switch.name}",
f"{entry.entry_id}_{switch.module_id}_{switch.data_id}", f"{entry.entry_id}_{switch.module_id}_{switch.key}",
) )
) )
@ -117,7 +114,7 @@ class PlenticoreDataSwitch(CoordinatorEntity, SwitchEntity, ABC):
platform_name: str, platform_name: str,
module_id: str, module_id: str,
data_id: str, data_id: str,
name: str, name: str | None,
is_on: str, is_on: str,
on_value: str, on_value: str,
on_label: str, on_label: str,