|
1 | 1 | """The Z-Wave-Me WS integration.""" |
2 | 2 |
|
3 | | -from zwave_me_ws import ZWaveMe, ZWaveMeData |
4 | | - |
5 | 3 | from homeassistant.config_entries import ConfigEntry |
6 | | -from homeassistant.const import CONF_TOKEN, CONF_URL |
7 | 4 | from homeassistant.core import HomeAssistant |
8 | 5 | from homeassistant.exceptions import ConfigEntryNotReady |
9 | 6 | from homeassistant.helpers import device_registry as dr |
10 | | -from homeassistant.helpers.dispatcher import dispatcher_send |
11 | | - |
12 | | -from .const import DOMAIN, PLATFORMS, ZWaveMePlatform |
13 | 7 |
|
14 | | -ZWAVE_ME_PLATFORMS = [platform.value for platform in ZWaveMePlatform] |
| 8 | +from .const import PLATFORMS |
| 9 | +from .controller import ZWaveMeConfigEntry, ZWaveMeController |
15 | 10 |
|
16 | 11 |
|
17 | | -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
| 12 | +async def async_setup_entry(hass: HomeAssistant, entry: ZWaveMeConfigEntry) -> bool: |
18 | 13 | """Set up Z-Wave-Me from a config entry.""" |
19 | | - hass.data.setdefault(DOMAIN, {}) |
20 | | - controller = hass.data[DOMAIN][entry.entry_id] = ZWaveMeController(hass, entry) |
21 | | - if await controller.async_establish_connection(): |
22 | | - await async_setup_platforms(hass, entry, controller) |
23 | | - registry = dr.async_get(hass) |
24 | | - controller.remove_stale_devices(registry) |
25 | | - return True |
26 | | - raise ConfigEntryNotReady |
| 14 | + controller = ZWaveMeController(hass, entry) |
| 15 | + |
| 16 | + if not await controller.async_establish_connection(): |
| 17 | + raise ConfigEntryNotReady |
| 18 | + |
| 19 | + entry.runtime_data = controller |
| 20 | + await async_setup_platforms(hass, entry, controller) |
| 21 | + registry = dr.async_get(hass) |
| 22 | + controller.remove_stale_devices(registry) |
| 23 | + return True |
27 | 24 |
|
28 | 25 |
|
29 | | -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
| 26 | +async def async_unload_entry(hass: HomeAssistant, entry: ZWaveMeConfigEntry) -> bool: |
30 | 27 | """Unload a config entry.""" |
31 | 28 |
|
32 | 29 | unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) |
33 | 30 | if unload_ok: |
34 | | - controller = hass.data[DOMAIN].pop(entry.entry_id) |
35 | | - await controller.zwave_api.close_ws() |
| 31 | + await entry.runtime_data.zwave_api.close_ws() |
36 | 32 | return unload_ok |
37 | 33 |
|
38 | 34 |
|
39 | | -class ZWaveMeController: |
40 | | - """Main ZWave-Me API class.""" |
41 | | - |
42 | | - def __init__(self, hass: HomeAssistant, config: ConfigEntry) -> None: |
43 | | - """Create the API instance.""" |
44 | | - self.device_ids: set = set() |
45 | | - self._hass = hass |
46 | | - self.config = config |
47 | | - self.zwave_api = ZWaveMe( |
48 | | - on_device_create=self.on_device_create, |
49 | | - on_device_update=self.on_device_update, |
50 | | - on_device_remove=self.on_device_unavailable, |
51 | | - on_device_destroy=self.on_device_destroy, |
52 | | - on_new_device=self.add_device, |
53 | | - token=self.config.data[CONF_TOKEN], |
54 | | - url=self.config.data[CONF_URL], |
55 | | - platforms=ZWAVE_ME_PLATFORMS, |
56 | | - ) |
57 | | - self.platforms_inited = False |
58 | | - |
59 | | - async def async_establish_connection(self): |
60 | | - """Get connection status.""" |
61 | | - return await self.zwave_api.get_connection() |
62 | | - |
63 | | - def add_device(self, device: ZWaveMeData) -> None: |
64 | | - """Send signal to create device.""" |
65 | | - if device.id in self.device_ids: |
66 | | - dispatcher_send(self._hass, f"ZWAVE_ME_INFO_{device.id}", device) |
67 | | - else: |
68 | | - dispatcher_send( |
69 | | - self._hass, f"ZWAVE_ME_NEW_{device.deviceType.upper()}", device |
70 | | - ) |
71 | | - self.device_ids.add(device.id) |
72 | | - |
73 | | - def on_device_create(self, devices: list[ZWaveMeData]) -> None: |
74 | | - """Create multiple devices.""" |
75 | | - for device in devices: |
76 | | - if device.deviceType in ZWAVE_ME_PLATFORMS and self.platforms_inited: |
77 | | - self.add_device(device) |
78 | | - |
79 | | - def on_device_update(self, new_info: ZWaveMeData) -> None: |
80 | | - """Send signal to update device.""" |
81 | | - dispatcher_send(self._hass, f"ZWAVE_ME_INFO_{new_info.id}", new_info) |
82 | | - |
83 | | - def on_device_unavailable(self, device_id: str) -> None: |
84 | | - """Send signal to set device unavailable.""" |
85 | | - dispatcher_send(self._hass, f"ZWAVE_ME_UNAVAILABLE_{device_id}") |
86 | | - |
87 | | - def on_device_destroy(self, device_id: str) -> None: |
88 | | - """Send signal to destroy device.""" |
89 | | - dispatcher_send(self._hass, f"ZWAVE_ME_DESTROY_{device_id}") |
90 | | - |
91 | | - def remove_stale_devices(self, registry: dr.DeviceRegistry): |
92 | | - """Remove old-format devices in the registry.""" |
93 | | - for device_id in self.device_ids: |
94 | | - device = registry.async_get_device( |
95 | | - identifiers={(DOMAIN, f"{self.config.unique_id}-{device_id}")} |
96 | | - ) |
97 | | - if device is not None: |
98 | | - registry.async_remove_device(device.id) |
99 | | - |
100 | | - |
101 | 35 | async def async_setup_platforms( |
102 | 36 | hass: HomeAssistant, entry: ConfigEntry, controller: ZWaveMeController |
103 | 37 | ) -> None: |
|
0 commit comments