Fibaro finish separation of scenes (#100734)
This commit is contained in:
parent
14b39c3bcf
commit
a2730fb29d
3 changed files with 11 additions and 16 deletions
|
@ -8,6 +8,7 @@ from typing import Any
|
||||||
|
|
||||||
from pyfibaro.fibaro_client import FibaroClient
|
from pyfibaro.fibaro_client import FibaroClient
|
||||||
from pyfibaro.fibaro_device import DeviceModel
|
from pyfibaro.fibaro_device import DeviceModel
|
||||||
|
from pyfibaro.fibaro_scene import SceneModel
|
||||||
from requests.exceptions import HTTPError
|
from requests.exceptions import HTTPError
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
@ -87,9 +88,11 @@ class FibaroController:
|
||||||
self._import_plugins = config[CONF_IMPORT_PLUGINS]
|
self._import_plugins = config[CONF_IMPORT_PLUGINS]
|
||||||
self._room_map = None # Mapping roomId to room object
|
self._room_map = None # Mapping roomId to room object
|
||||||
self._device_map = None # Mapping deviceId to device object
|
self._device_map = None # Mapping deviceId to device object
|
||||||
self.fibaro_devices: dict[Platform, list] = defaultdict(
|
self.fibaro_devices: dict[Platform, list[DeviceModel]] = defaultdict(
|
||||||
list
|
list
|
||||||
) # List of devices by entity platform
|
) # List of devices by entity platform
|
||||||
|
# All scenes
|
||||||
|
self._scenes: list[SceneModel] = []
|
||||||
self._callbacks: dict[Any, Any] = {} # Update value callbacks by deviceId
|
self._callbacks: dict[Any, Any] = {} # Update value callbacks by deviceId
|
||||||
self.hub_serial: str # Unique serial number of the hub
|
self.hub_serial: str # Unique serial number of the hub
|
||||||
self.hub_name: str # The friendly name of the hub
|
self.hub_name: str # The friendly name of the hub
|
||||||
|
@ -115,7 +118,7 @@ class FibaroController:
|
||||||
|
|
||||||
self._room_map = {room.fibaro_id: room for room in self._client.read_rooms()}
|
self._room_map = {room.fibaro_id: room for room in self._client.read_rooms()}
|
||||||
self._read_devices()
|
self._read_devices()
|
||||||
self._read_scenes()
|
self._scenes = self._client.read_scenes()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def connect_with_error_handling(self) -> None:
|
def connect_with_error_handling(self) -> None:
|
||||||
|
@ -282,12 +285,9 @@ class FibaroController:
|
||||||
room = self._room_map.get(room_id)
|
room = self._room_map.get(room_id)
|
||||||
return room.name if room else None
|
return room.name if room else None
|
||||||
|
|
||||||
def _read_scenes(self):
|
def read_scenes(self) -> list[SceneModel]:
|
||||||
scenes = self._client.read_scenes()
|
"""Return list of scenes."""
|
||||||
for device in scenes:
|
return self._scenes
|
||||||
device.fibaro_controller = self
|
|
||||||
self.fibaro_devices[Platform.SCENE].append(device)
|
|
||||||
_LOGGER.debug("Scene -> %s", device)
|
|
||||||
|
|
||||||
def _read_devices(self):
|
def _read_devices(self):
|
||||||
"""Read and process the device list."""
|
"""Read and process the device list."""
|
||||||
|
|
|
@ -7,7 +7,6 @@ from pyfibaro.fibaro_scene import SceneModel
|
||||||
|
|
||||||
from homeassistant.components.scene import Scene
|
from homeassistant.components.scene import Scene
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import Platform
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
@ -25,7 +24,7 @@ async def async_setup_entry(
|
||||||
"""Perform the setup for Fibaro scenes."""
|
"""Perform the setup for Fibaro scenes."""
|
||||||
controller: FibaroController = hass.data[DOMAIN][entry.entry_id]
|
controller: FibaroController = hass.data[DOMAIN][entry.entry_id]
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[FibaroScene(scene) for scene in controller.fibaro_devices[Platform.SCENE]],
|
[FibaroScene(scene, controller) for scene in controller.read_scenes()],
|
||||||
True,
|
True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -33,11 +32,10 @@ async def async_setup_entry(
|
||||||
class FibaroScene(Scene):
|
class FibaroScene(Scene):
|
||||||
"""Representation of a Fibaro scene entity."""
|
"""Representation of a Fibaro scene entity."""
|
||||||
|
|
||||||
def __init__(self, fibaro_scene: SceneModel) -> None:
|
def __init__(self, fibaro_scene: SceneModel, controller: FibaroController) -> None:
|
||||||
"""Initialize the Fibaro scene."""
|
"""Initialize the Fibaro scene."""
|
||||||
self._fibaro_scene = fibaro_scene
|
self._fibaro_scene = fibaro_scene
|
||||||
|
|
||||||
controller: FibaroController = fibaro_scene.fibaro_controller
|
|
||||||
room_name = controller.get_room_name(fibaro_scene.room_id)
|
room_name = controller.get_room_name(fibaro_scene.room_id)
|
||||||
if not room_name:
|
if not room_name:
|
||||||
room_name = "Unknown"
|
room_name = "Unknown"
|
||||||
|
|
|
@ -47,10 +47,7 @@ async def setup_platform(
|
||||||
controller_mock = Mock()
|
controller_mock = Mock()
|
||||||
controller_mock.hub_serial = "HC2-111111"
|
controller_mock.hub_serial = "HC2-111111"
|
||||||
controller_mock.get_room_name.return_value = room_name
|
controller_mock.get_room_name.return_value = room_name
|
||||||
controller_mock.fibaro_devices = {Platform.SCENE: scenes}
|
controller_mock.read_scenes.return_value = scenes
|
||||||
|
|
||||||
for scene in scenes:
|
|
||||||
scene.fibaro_controller = controller_mock
|
|
||||||
|
|
||||||
hass.data[DOMAIN] = {config_entry.entry_id: controller_mock}
|
hass.data[DOMAIN] = {config_entry.entry_id: controller_mock}
|
||||||
await hass.config_entries.async_forward_entry_setup(config_entry, platform)
|
await hass.config_entries.async_forward_entry_setup(config_entry, platform)
|
||||||
|
|
Loading…
Add table
Reference in a new issue