Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Description dataclasses to frozen #730

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
frenck marked this conversation as resolved.
Show resolved Hide resolved

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
frenck marked this conversation as resolved.
Show resolved Hide resolved
is_on_fn: Callable[[Cloud[CloudClient]], bool | None]
set_fn: Callable[[Cloud[CloudClient], bool], Awaitable[Any]]
frenck marked this conversation as resolved.
Show resolved Hide resolved


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
Comment on lines 138 to +140
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With enabled this results in an call-arg error. set_fn is only typed as a Callback. The alternative would be to use a callback protocol which would allow for named arguments.

.../cloud/switch.py:140: error: Unexpected keyword argument "enabled"  [call-arg]


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 @@ -47,7 +47,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback


@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