Skip to content

Commit

Permalink
Add scene entities for presets
Browse files Browse the repository at this point in the history
  • Loading branch information
schmittx committed Aug 7, 2023
1 parent e156a28 commit 90bfad7
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion custom_components/hatch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
)

PLATFORMS = [
Platform.BUTTON,
Platform.LIGHT,
Platform.MEDIA_PLAYER,
Platform.NUMBER,
Platform.SCENE,
Platform.SENSOR,
Platform.SWITCH,
]
Expand Down
1 change: 0 additions & 1 deletion custom_components/hatch/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
class HatchBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Class to describe a Hatch binary sensor entity."""


BINARY_SENSOR_DESCRIPTIONS: list[HatchBinarySensorEntityDescription] = [
HatchBinarySensorEntityDescription(
key="is_light_on",
Expand Down
1 change: 0 additions & 1 deletion custom_components/hatch/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class HatchLightEntityDescription(LightEntityDescription):

translation_key: str | None = "all"


LIGHT_DESCRIPTIONS: list[HatchLightEntityDescription] = [
HatchLightEntityDescription(
key="nightlight",
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hatch/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"aiohttp>=3.8.1",
"distro>=1.0.0"
],
"version": "1.0.1"
"version": "1.1.0"
}
1 change: 0 additions & 1 deletion custom_components/hatch/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
class HatchMediaPlayerEntityDescription(MediaPlayerEntityDescription):
"""Class to describe a Hatch media player entity."""


MEDIA_PLAYER_DESCRIPTIONS: list[HatchMediaPlayerEntityDescription] = [
HatchMediaPlayerEntityDescription(
key="sound_machine",
Expand Down
1 change: 0 additions & 1 deletion custom_components/hatch/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
class HatchNumberEntityDescription(NumberEntityDescription):
"""Class to describe a Hatch number."""


NUMBER_DESCRIPTIONS: list[HatchNumberEntityDescription] = [
HatchNumberEntityDescription(
key="clock_brightness",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Support for Hatch button entities."""
"""Support for Hatch scene entities."""
from __future__ import annotations

from collections.abc import Mapping
from dataclasses import dataclass
import logging
from typing import Any

from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.components.light import (
ATTR_BRIGHTNESS_PCT,
ATTR_COLOR_MODE,
Expand All @@ -18,8 +17,10 @@
ATTR_MEDIA_VOLUME_LEVEL,
ATTR_SOUND_MODE,
)
from homeassistant.components.scene import Scene
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory, EntityDescription
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import HatchEntity
Expand All @@ -28,9 +29,8 @@
from .const import DOMAIN, DEVICES, EFFECT_RAINBOW, ENTITIES

@dataclass
class HatchButtonEntityDescription(ButtonEntityDescription):
"""Class to describe a Hatch button entity."""

class HatchSceneEntityDescription(EntityDescription):
"""Class to describe a Hatch scene entity."""

_LOGGER = logging.getLogger(__name__)

Expand All @@ -40,24 +40,25 @@ async def async_setup_entry(
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up a Hatch button entity based on a config entry."""
"""Set up a Hatch scene entity based on a config entry."""
entry = hass.data[DOMAIN][config_entry.entry_id]
devices = entry[DEVICES]
entities: list[HatchButtonEntity] = []
entities: list[HatchSceneEntity] = []

for device in devices:
if hasattr(device, "presets"):
for preset in device.presets:
if preset.is_favorite:
_LOGGER.debug(
f"[{device.info.name}] Found preset button entity: {preset.index}",
f"[{device.info.name}] Found preset scene entity: {preset.index}",
)
entities.append(
HatchButtonEntity(
HatchSceneEntity(
device=device,
entity_description=HatchButtonEntityDescription(
entity_description=HatchSceneEntityDescription(
key=None,
name=None,
entity_category=EntityCategory.CONFIG,
),
preset=preset,
)
Expand All @@ -67,15 +68,15 @@ async def async_setup_entry(
async_add_entities(entities)


class HatchButtonEntity(HatchEntity, ButtonEntity):
"""Representation of a Hatch button entity."""
class HatchSceneEntity(HatchEntity, Scene):
"""Representation of a Hatch scene entity."""

entity_description: HatchButtonEntityDescription
entity_description: HatchSceneEntityDescription

def __init__(
self,
device: HatchDevice,
entity_description: HatchButtonEntityDescription=None,
entity_description: HatchSceneEntityDescription=None,
preset: HatchPreset=None,
) -> None:
"""Initialize device."""
Expand All @@ -92,8 +93,8 @@ def unique_id(self) -> str:
"""Return a unique ID."""
return f"{super().unique_id}-preset-{self.preset.index}"

def press(self) -> None:
"""Press the button."""
def activate(self, **kwargs: Any) -> None:
"""Activate scene. Try to get entities into requested state."""
self.device.set_preset(self.preset)

@property
Expand Down
1 change: 0 additions & 1 deletion custom_components/hatch/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
class HatchSensorEntityDescription(SensorEntityDescription):
"""Class to describe a Hatch sensor entity."""


SENSOR_DESCRIPTIONS: list[HatchSensorEntityDescription] = [
HatchSensorEntityDescription(
key="battery_level",
Expand Down
4 changes: 3 additions & 1 deletion custom_components/hatch/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"all": {
"state_attributes": {
"effect": {
"rainbow": "Rainbow"
"state": {
"rainbow": "Rainbow"
}
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions custom_components/hatch/switch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Support for Hatch switch entities."""
from __future__ import annotations

from collections.abc import Mapping
from collections.abc import Callable, Mapping
from dataclasses import dataclass
import logging
from typing import Any
Expand All @@ -21,12 +21,17 @@
class HatchSwitchEntityDescription(SwitchEntityDescription):
"""Class to describe a Hatch switch entity."""

extra_attrs: dict[str, Callable] | None = None

SWITCH_DESCRIPTIONS: list[HatchSwitchEntityDescription] = [
HatchSwitchEntityDescription(
key="is_device_on",
name="Power",
entity_category=None,
extra_attrs={
"active_preset_index": lambda device: getattr(device, "active_preset_index"),
"active_program_name": lambda device: getattr(device, "active_program_name"),
},
),
HatchSwitchEntityDescription(
key="clock_enabled",
Expand Down Expand Up @@ -146,8 +151,8 @@ def extra_state_attributes(self) -> Mapping[str, Any] | None:
is lowercase snake_case.
"""
attrs = {}
if self.device.active_preset_index:
attrs["active_preset_index"] = self.device.active_preset_index
if self.device.active_program_name:
attrs["active_program_name"] = self.device.active_program_name
if self.entity_description.extra_attrs and self.is_on:
for key, func in self.entity_description.extra_attrs.items():
if value := func(self.device):
attrs[key] = value
return attrs
4 changes: 3 additions & 1 deletion custom_components/hatch/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"all": {
"state_attributes": {
"effect": {
"rainbow": "Rainbow"
"state": {
"rainbow": "Rainbow"
}
}
}
}
Expand Down

0 comments on commit 90bfad7

Please sign in to comment.