Skip to content

Commit

Permalink
Improve select entity and add support for setting Simple Memorized Vo…
Browse files Browse the repository at this point in the history
…lume (#642)

* Improve select entity

* Update select.py

* Improve codestyle

* Add entity category
  • Loading branch information
iMicknl authored Nov 19, 2021
1 parent 90f40e2 commit 1c30822
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 39 deletions.
2 changes: 2 additions & 0 deletions custom_components/tahoma/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from homeassistant.components.lock import DOMAIN as LOCK
from homeassistant.components.number import DOMAIN as NUMBER
from homeassistant.components.scene import DOMAIN as SCENE
from homeassistant.components.select import DOMAIN as SELECT
from homeassistant.components.sensor import DOMAIN as SENSOR
from homeassistant.components.switch import DOMAIN as SWITCH
from homeassistant.components.water_heater import DOMAIN as WATER_HEATER
Expand All @@ -30,6 +31,7 @@
LOCK,
NUMBER,
SCENE,
SELECT,
SENSOR,
SWITCH,
WATER_HEATER,
Expand Down
12 changes: 11 additions & 1 deletion custom_components/tahoma/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from homeassistant.components.binary_sensor import BinarySensorEntityDescription
from homeassistant.components.number import NumberEntityDescription
from homeassistant.components.select import SelectEntityDescription
from homeassistant.components.sensor import SensorEntityDescription
from homeassistant.const import ATTR_BATTERY_LEVEL
from homeassistant.helpers.entity import DeviceInfo
Expand Down Expand Up @@ -145,6 +146,14 @@ class OverkizNumberDescription(NumberEntityDescription):
state: Callable[[str], bool] = lambda state: state


@dataclass
class OverkizSelectDescription(SelectEntityDescription):
"""Class to describe an Overkiz select entity."""

options: list[str] = None
select_option: Callable[[str], bool] = lambda option, execute_command: None


class OverkizDescriptiveEntity(OverkizEntity):
"""Representation of a Overkiz device entity based on a description."""

Expand All @@ -154,7 +163,8 @@ def __init__(
coordinator: OverkizDataUpdateCoordinator,
description: OverkizSensorDescription
| OverkizBinarySensorDescription
| OverkizNumberDescription,
| OverkizNumberDescription
| OverkizSelectDescription,
):
"""Initialize the device."""
super().__init__(device_url, coordinator)
Expand Down
93 changes: 55 additions & 38 deletions custom_components/tahoma/select.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
"""Support for Overkiz select devices."""
from homeassistant.components.cover import DOMAIN as COVER
from __future__ import annotations

from homeassistant.components.select import SelectEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ENTITY_CATEGORY_CONFIG
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from custom_components.tahoma.coordinator import OverkizDataUpdateCoordinator
from custom_components.tahoma.cover_devices.tahoma_cover import (
COMMAND_CLOSE,
COMMAND_OPEN,
)

from .const import DOMAIN
from .entity import OverkizEntity

CORE_OPEN_CLOSED_PEDESTRIAN_STATE = "core:OpenClosedPedestrianState"
COMMAND_SET_PEDESTRIAN_POSITION = "setPedestrianPosition"
from .entity import OverkizDescriptiveEntity, OverkizSelectDescription

OPTION_TO_COMMAND = {
"closed": COMMAND_CLOSE,
"open": COMMAND_OPEN,
"pedestrian": COMMAND_SET_PEDESTRIAN_POSITION,
}
SELECT_DESCRIPTIONS = [
OverkizSelectDescription(
key="core:OpenClosedPedestrianState",
name="Position",
icon="mdi:content-save-cog",
options=["closed", "open", "pedestrian"],
select_option=lambda option, execute_command: execute_command(
{
"closed": "close",
"open": "open",
"pedestrian": "setPedestrianPosition",
}[option]
),
),
OverkizSelectDescription(
key="io:MemorizedSimpleVolumeState",
name="Memorized Simple Volume",
icon="mdi:volume-high",
options=["highest", "standard"],
select_option=lambda option, execute_command: execute_command(
"setMemorizedSimpleVolume", option
),
entity_category=ENTITY_CATEGORY_CONFIG,
),
]


async def async_setup_entry(
Expand All @@ -33,40 +46,44 @@ async def async_setup_entry(
data = hass.data[DOMAIN][entry.entry_id]
coordinator = data["coordinator"]

entities = [
PedestrianGateSelect(device.device_url, coordinator)
for device in data["platforms"][COVER]
if CORE_OPEN_CLOSED_PEDESTRIAN_STATE in device.states
]
entities = []

async_add_entities(entities)
key_supported_states = {
description.key: description for description in SELECT_DESCRIPTIONS
}

for device in coordinator.data.values():
for state in device.definition.states:
if description := key_supported_states.get(state.qualified_name):
entities.append(
OverkizSelect(
device.device_url,
coordinator,
description,
)
)

class PedestrianGateSelect(OverkizEntity, SelectEntity):
"""Representation of the various state for a pedestrian gate."""
async_add_entities(entities)

_attr_icon = "mdi:content-save-cog"

def __init__(
self,
device_url: str,
coordinator: OverkizDataUpdateCoordinator,
):
"""Initialize the device."""
super().__init__(device_url, coordinator)
self._attr_name = f"{super().name} Position"
class OverkizSelect(OverkizDescriptiveEntity, SelectEntity):
"""Representation of an Overkiz Number entity."""

@property
def current_option(self):
def current_option(self) -> str | None:
"""Return the selected entity option to represent the entity state."""
return self.device.states.get(CORE_OPEN_CLOSED_PEDESTRIAN_STATE).value
if state := self.device.states.get(self.entity_description.key):
return state.value

return None

@property
def options(self):
"""Return a set of selectable options."""
return ["closed", "open", "pedestrian"]
return self.entity_description.options

async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
if option in OPTION_TO_COMMAND:
await self.executor.async_execute_command(OPTION_TO_COMMAND[option])
await self.entity_description.select_option(
option, self.executor.async_execute_command
)

0 comments on commit 1c30822

Please sign in to comment.