Skip to content

Commit

Permalink
Change Description dataclasses to frozen (#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Apr 23, 2024
1 parent 03558da commit 0d8bd7e
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 23 deletions.
10 changes: 6 additions & 4 deletions custom_components/spook/ectoplasms/cloud/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
from typing import TYPE_CHECKING

from homeassistant.components.cloud.const import DOMAIN as CLOUD_DOMAIN
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.device_registry import DeviceInfo

from ...const import DOMAIN
from ...entity import SpookEntity, SpookEntityDescription

if TYPE_CHECKING:
from hass_nabucasa import Cloud

from homeassistant.components.cloud.client import CloudClient


class HomeAssistantCloudSpookEntity(SpookEntity):
"""Defines an base Spook entity for Home Assistant Cloud related entities."""

_cloud: Cloud

def __init__(self, cloud: Cloud, description: SpookEntityDescription) -> None:
def __init__(
self, cloud: Cloud[CloudClient], description: SpookEntityDescription
) -> None:
"""Initialize the entity."""
super().__init__(description=description)
self._cloud = cloud
Expand Down
23 changes: 9 additions & 14 deletions custom_components/spook/ectoplasms/cloud/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,21 @@

from hass_nabucasa import Cloud

from homeassistant.components.cloud.client import CloudClient
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback


@dataclass(kw_only=True)
@dataclass(frozen=True, kw_only=True)
class HomeAssistantCloudSpookSwitchEntityDescription(
SpookEntityDescription,
SwitchEntityDescription,
):
"""Class describing Spook Home Assistant sensor entities."""

icon_off: str | None = None
is_on_fn: Callable[[Cloud], bool | None]
set_fn: Callable[[Cloud, bool], Awaitable[Any]]

def __post_init__(self) -> None:
"""Sync icon_off with icon."""
if self.icon_off is None:
self.icon_off = self.icon
is_on_fn: Callable[[Cloud[CloudClient]], bool | None]
set_fn: Callable[[Cloud[CloudClient], bool], Awaitable[Any]]


SWITCHES: tuple[HomeAssistantCloudSpookSwitchEntityDescription, ...] = (
Expand Down Expand Up @@ -105,7 +100,7 @@ async def async_setup_entry(
) -> None:
"""Set up Spook Home Assistant Cloud switches."""
if CLOUD_DOMAIN in hass.config.components:
cloud: Cloud = hass.data[CLOUD_DOMAIN]
cloud: Cloud[CloudClient] = hass.data[CLOUD_DOMAIN]
async_add_entities(
HomeAssistantCloudSpookSwitchEntity(cloud, description)
for description in SWITCHES
Expand All @@ -131,8 +126,8 @@ async def _update_state(_: Any) -> None:
@property
def icon(self) -> str | None:
"""Return the icon."""
if self.entity_description.icon_off and self.is_on is False:
return self.entity_description.icon_off
if self.entity_description.icon and self.is_on is False:
return self.entity_description.icon
return super().icon

@property
Expand All @@ -142,8 +137,8 @@ def is_on(self) -> bool | None:

async def async_turn_on(self, **_kwargs: Any) -> None:
"""Turn the entity on."""
await self.entity_description.set_fn(self._cloud, enabled=True)
await self.entity_description.set_fn(self._cloud, True) # noqa: FBT003

async def async_turn_off(self, **_kwargs: Any) -> None:
"""Turn the entity off."""
await self.entity_description.set_fn(self._cloud, enabled=False)
await self.entity_description.set_fn(self._cloud, False) # noqa: FBT003
2 changes: 1 addition & 1 deletion custom_components/spook/ectoplasms/homeassistant/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback


@dataclass(kw_only=True)
@dataclass(frozen=True, kw_only=True)
class HomeAssistantSpookButtonEntityDescription(
SpookEntityDescription,
ButtonEntityDescription,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/spook/ectoplasms/homeassistant/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from homeassistant.util.event_type import EventType


@dataclass(kw_only=True)
@dataclass(frozen=True, kw_only=True)
class HomeAssistantSpookSensorEntityDescription(
SpookEntityDescription,
SensorEntityDescription,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/spook/ectoplasms/repairs/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback


@dataclass(kw_only=True)
@dataclass(frozen=True, kw_only=True)
class RepairsSpookButtonEntityDescription(
SpookEntityDescription,
ButtonEntityDescription,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/spook/ectoplasms/repairs/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback


@dataclass
@dataclass(frozen=True, kw_only=True)
class RepairsSpookEventEntityDescription(
SpookEntityDescription,
EventEntityDescription,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/spook/ectoplasms/repairs/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback


@dataclass(kw_only=True)
@dataclass(frozen=True, kw_only=True)
class RepairsSpookSensorEntityDescription(
SpookEntityDescription,
SensorEntityDescription,
Expand Down

0 comments on commit 0d8bd7e

Please sign in to comment.