1 """The Hunter Douglas PowerView integration."""
4 from typing
import TYPE_CHECKING
6 from aiopvapi.resources.model
import PowerviewData
7 from aiopvapi.rooms
import Rooms
8 from aiopvapi.scenes
import Scenes
9 from aiopvapi.shades
import Shades
16 from .const
import DOMAIN, HUB_EXCEPTIONS
17 from .coordinator
import PowerviewShadeUpdateCoordinator
18 from .model
import PowerviewConfigEntry, PowerviewEntryData
19 from .shade_data
import PowerviewShadeData
20 from .util
import async_connect_hub
33 _LOGGER = logging.getLogger(__name__)
37 """Set up Hunter Douglas PowerView from a config entry."""
39 hub_address: str = config[CONF_HOST]
40 api_version: int |
None = config.get(CONF_API_VERSION)
41 _LOGGER.debug(
"Connecting %s at %s with v%s api", DOMAIN, hub_address, api_version)
46 except HUB_EXCEPTIONS
as err:
48 f
"Connection error to PowerView hub {hub_address}: {err}"
52 pv_request = api.pv_request
53 device_info = api.device_info
55 if hub.role !=
"Primary":
59 "%s (%s) is performing role of %s Hub. "
60 "Only the Primary Hub can manage shades",
68 rooms = Rooms(pv_request)
69 room_data: PowerviewData = await rooms.get_rooms()
71 scenes = Scenes(pv_request)
72 scene_data: PowerviewData = await scenes.get_scenes()
74 shades = Shades(pv_request)
75 shade_data: PowerviewData = await shades.get_shades()
76 except HUB_EXCEPTIONS
as err:
78 f
"Connection error to PowerView hub {hub_address}: {err}"
84 if CONF_API_VERSION
not in config:
85 new_data = {**entry.data}
86 new_data[CONF_API_VERSION] = hub.api_version
87 hass.config_entries.async_update_entry(entry, data=new_data)
89 if entry.unique_id
is None:
90 hass.config_entries.async_update_entry(
91 entry, unique_id=device_info.serial_number
97 coordinator.data.store_group_data(shade_data)
101 room_data=room_data.processed,
102 scene_data=scene_data.processed,
103 shade_data=shade_data.processed,
104 coordinator=coordinator,
105 device_info=device_info,
108 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
114 """Unload a config entry."""
115 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
121 _LOGGER.debug(
"Migrating from version %s.%s", entry.version, entry.minor_version)
123 if entry.version == 1:
125 if entry.minor_version == 1:
126 if entry.unique_id
is None:
129 hass.config_entries.async_update_entry(entry, minor_version=2)
131 _LOGGER.debug(
"Migrated to version %s.%s", entry.version, entry.minor_version)
137 hass: HomeAssistant, entry: PowerviewConfigEntry
139 """Add the unique id if its missing."""
140 address: str = entry.data[CONF_HOST]
141 api_version: int |
None = entry.data.get(CONF_API_VERSION)
143 hass.config_entries.async_update_entry(
144 entry, unique_id=api.device_info.serial_number
149 """Migrate int based unique ids to str."""
150 entity_registry = er.async_get(hass)
151 registry_entries = er.async_entries_for_config_entry(
152 entity_registry, entry.entry_id
155 assert entry.unique_id
156 for reg_entry
in registry_entries:
157 if isinstance(reg_entry.unique_id, int)
or (
158 isinstance(reg_entry.unique_id, str)
159 and not reg_entry.unique_id.startswith(entry.unique_id)
162 "Migrating %s: %s to %s_%s",
168 entity_registry.async_update_entity(
170 new_unique_id=f
"{entry.unique_id}_{reg_entry.unique_id}",
PowerviewAPI async_connect_hub(HomeAssistant hass, str address, int|None api_version=None)
None _async_add_missing_entry_unique_id(HomeAssistant hass, PowerviewConfigEntry entry)
None _migrate_unique_ids(HomeAssistant hass, PowerviewConfigEntry entry)
bool async_migrate_entry(HomeAssistant hass, PowerviewConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, PowerviewConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, PowerviewConfigEntry entry)