Skip to content

Commit

Permalink
Migrate hunterdouglas_powerview to use entry.runtime_data (#121887)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jul 13, 2024
1 parent 2dec713 commit 62613af
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 69 deletions.
13 changes: 5 additions & 8 deletions homeassistant/components/hunterdouglas_powerview/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from aiopvapi.scenes import Scenes
from aiopvapi.shades import Shades

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_VERSION, CONF_HOST, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
Expand All @@ -18,7 +17,7 @@

from .const import DOMAIN, HUB_EXCEPTIONS
from .coordinator import PowerviewShadeUpdateCoordinator
from .model import PowerviewDeviceInfo, PowerviewEntryData
from .model import PowerviewConfigEntry, PowerviewDeviceInfo, PowerviewEntryData
from .shade_data import PowerviewShadeData

PARALLEL_UPDATES = 1
Expand All @@ -36,7 +35,7 @@
_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: PowerviewConfigEntry) -> bool:
"""Set up Hunter Douglas PowerView from a config entry."""

config = entry.data
Expand Down Expand Up @@ -100,7 +99,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# populate raw shade data into the coordinator for diagnostics
coordinator.data.store_group_data(shade_data)

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = PowerviewEntryData(
entry.runtime_data = PowerviewEntryData(
api=pv_request,
room_data=room_data.processed,
scene_data=scene_data.processed,
Expand All @@ -126,8 +125,6 @@ async def async_get_device_info(hub: Hub) -> PowerviewDeviceInfo:
)


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: PowerviewConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
10 changes: 3 additions & 7 deletions homeassistant/components/hunterdouglas_powerview/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
ButtonEntity,
ButtonEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN
from .coordinator import PowerviewShadeUpdateCoordinator
from .entity import ShadeEntity
from .model import PowerviewDeviceInfo, PowerviewEntryData
from .model import PowerviewConfigEntry, PowerviewDeviceInfo


@dataclass(frozen=True)
Expand Down Expand Up @@ -75,13 +73,11 @@ class PowerviewButtonDescription(

async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: PowerviewConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the hunter douglas advanced feature buttons."""

pv_entry: PowerviewEntryData = hass.data[DOMAIN][entry.entry_id]

pv_entry = entry.runtime_data
entities: list[ButtonEntity] = []
for shade in pv_entry.shade_data.values():
room_name = getattr(pv_entry.room_data.get(shade.room_id), ATTR_NAME, "")
Expand Down
14 changes: 7 additions & 7 deletions homeassistant/components/hunterdouglas_powerview/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@
CoverEntity,
CoverEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_call_later

from .const import DOMAIN, STATE_ATTRIBUTE_ROOM_NAME
from .const import STATE_ATTRIBUTE_ROOM_NAME
from .coordinator import PowerviewShadeUpdateCoordinator
from .entity import ShadeEntity
from .model import PowerviewDeviceInfo, PowerviewEntryData
from .model import PowerviewConfigEntry, PowerviewDeviceInfo

_LOGGER = logging.getLogger(__name__)

Expand All @@ -49,12 +48,13 @@


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant,
entry: PowerviewConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the hunter douglas shades."""

pv_entry: PowerviewEntryData = hass.data[DOMAIN][entry.entry_id]
coordinator: PowerviewShadeUpdateCoordinator = pv_entry.coordinator
pv_entry = entry.runtime_data
coordinator = pv_entry.coordinator

async def _async_initial_refresh() -> None:
"""Force position refresh shortly after adding.
Expand Down
16 changes: 6 additions & 10 deletions homeassistant/components/hunterdouglas_powerview/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
from __future__ import annotations

from dataclasses import asdict
import logging
from typing import Any

import attr

from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_CONFIGURATION_URL, CONF_HOST
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.device_registry import DeviceEntry

from .const import DOMAIN, REDACT_HUB_ADDRESS, REDACT_MAC_ADDRESS, REDACT_SERIAL_NUMBER
from .model import PowerviewEntryData
from .const import REDACT_HUB_ADDRESS, REDACT_MAC_ADDRESS, REDACT_SERIAL_NUMBER
from .model import PowerviewConfigEntry

REDACT_CONFIG = {
CONF_HOST,
Expand All @@ -26,11 +24,9 @@
ATTR_CONFIGURATION_URL,
}

_LOGGER = logging.getLogger(__name__)


async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry
hass: HomeAssistant, entry: PowerviewConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
data = _async_get_diagnostics(hass, entry)
Expand All @@ -47,7 +43,7 @@ async def async_get_config_entry_diagnostics(


async def async_get_device_diagnostics(
hass: HomeAssistant, entry: ConfigEntry, device: DeviceEntry
hass: HomeAssistant, entry: PowerviewConfigEntry, device: DeviceEntry
) -> dict[str, Any]:
"""Return diagnostics for a device entry."""
data = _async_get_diagnostics(hass, entry)
Expand All @@ -65,10 +61,10 @@ async def async_get_device_diagnostics(
@callback
def _async_get_diagnostics(
hass: HomeAssistant,
entry: ConfigEntry,
entry: PowerviewConfigEntry,
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
pv_entry: PowerviewEntryData = hass.data[DOMAIN][entry.entry_id]
pv_entry = entry.runtime_data
shade_data = pv_entry.coordinator.data.get_all_raw_data()
hub_info = async_redact_data(asdict(pv_entry.device_info), REDACT_CONFIG)
return {"hub_info": hub_info, "shade_data": shade_data}
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/hunterdouglas_powerview/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
from aiopvapi.resources.scene import Scene
from aiopvapi.resources.shade import BaseShade

from homeassistant.config_entries import ConfigEntry

from .coordinator import PowerviewShadeUpdateCoordinator

type PowerviewConfigEntry = ConfigEntry[PowerviewEntryData]


@dataclass
class PowerviewEntryData:
Expand Down
15 changes: 5 additions & 10 deletions homeassistant/components/hunterdouglas_powerview/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from collections.abc import Callable
from dataclasses import dataclass
import logging
from typing import Final

from aiopvapi.helpers.constants import ATTR_NAME, MOTION_VELOCITY
Expand All @@ -13,17 +12,13 @@
NumberMode,
RestoreNumber,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN
from .coordinator import PowerviewShadeUpdateCoordinator
from .entity import ShadeEntity
from .model import PowerviewDeviceInfo, PowerviewEntryData

_LOGGER = logging.getLogger(__name__)
from .model import PowerviewConfigEntry, PowerviewDeviceInfo


@dataclass(frozen=True, kw_only=True)
Expand Down Expand Up @@ -57,12 +52,12 @@ def store_velocity(


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant,
entry: PowerviewConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the hunter douglas number entities."""

pv_entry: PowerviewEntryData = hass.data[DOMAIN][entry.entry_id]

pv_entry = entry.runtime_data
entities: list[PowerViewNumber] = []
for shade in pv_entry.shade_data.values():
room_name = getattr(pv_entry.room_data.get(shade.room_id), ATTR_NAME, "")
Expand Down
13 changes: 6 additions & 7 deletions homeassistant/components/hunterdouglas_powerview/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,26 @@
from aiopvapi.resources.scene import Scene as PvScene

from homeassistant.components.scene import Scene
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN, STATE_ATTRIBUTE_ROOM_NAME
from .const import STATE_ATTRIBUTE_ROOM_NAME
from .coordinator import PowerviewShadeUpdateCoordinator
from .entity import HDEntity
from .model import PowerviewDeviceInfo, PowerviewEntryData
from .model import PowerviewConfigEntry, PowerviewDeviceInfo

_LOGGER = logging.getLogger(__name__)

RESYNC_DELAY = 60


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant,
entry: PowerviewConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up powerview scene entries."""

pv_entry: PowerviewEntryData = hass.data[DOMAIN][entry.entry_id]

pv_entry = entry.runtime_data
pvscenes: list[PowerViewScene] = []
for scene in pv_entry.scene_data.values():
room_name = getattr(pv_entry.room_data.get(scene.room_id), ATTR_NAME, "")
Expand Down
15 changes: 5 additions & 10 deletions homeassistant/components/hunterdouglas_powerview/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,19 @@

from collections.abc import Callable, Coroutine
from dataclasses import dataclass
import logging
from typing import Any, Final

from aiopvapi.helpers.constants import ATTR_NAME, FUNCTION_SET_POWER
from aiopvapi.resources.shade import BaseShade

from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN
from .coordinator import PowerviewShadeUpdateCoordinator
from .entity import ShadeEntity
from .model import PowerviewDeviceInfo, PowerviewEntryData

_LOGGER = logging.getLogger(__name__)
from .model import PowerviewConfigEntry, PowerviewDeviceInfo


@dataclass(frozen=True)
Expand Down Expand Up @@ -57,12 +52,12 @@ class PowerviewSelectDescription(


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant,
entry: PowerviewConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the hunter douglas select entities."""

pv_entry: PowerviewEntryData = hass.data[DOMAIN][entry.entry_id]

pv_entry = entry.runtime_data
entities: list[PowerViewSelect] = []
for shade in pv_entry.shade_data.values():
if not shade.has_battery_info():
Expand Down
12 changes: 5 additions & 7 deletions homeassistant/components/hunterdouglas_powerview/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, SIGNAL_STRENGTH_DECIBELS, EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN
from .coordinator import PowerviewShadeUpdateCoordinator
from .entity import ShadeEntity
from .model import PowerviewDeviceInfo, PowerviewEntryData
from .model import PowerviewConfigEntry, PowerviewDeviceInfo


@dataclass(frozen=True)
Expand Down Expand Up @@ -79,12 +77,12 @@ def get_signal_native_unit(shade: BaseShade) -> str:


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant,
entry: PowerviewConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the hunter douglas sensor entities."""

pv_entry: PowerviewEntryData = hass.data[DOMAIN][entry.entry_id]

pv_entry = entry.runtime_data
entities: list[PowerViewSensor] = []
for shade in pv_entry.shade_data.values():
room_name = getattr(pv_entry.room_data.get(shade.room_id), ATTR_NAME, "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
from __future__ import annotations

from dataclasses import fields
import logging
from typing import Any

from aiopvapi.resources.model import PowerviewData
from aiopvapi.resources.shade import BaseShade, ShadePosition

from .util import async_map_data_by_id

_LOGGER = logging.getLogger(__name__)

POSITION_FIELDS = [field for field in fields(ShadePosition) if field.name != "velocity"]


Expand Down

0 comments on commit 62613af

Please sign in to comment.