Move and rename fibaro base entity to separate module (#126055)

This commit is contained in:
epenet 2024-09-16 15:29:29 +02:00 committed by GitHub
parent 4c5535d1cc
commit 45f2198972
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 160 additions and 146 deletions

View file

@ -15,14 +15,7 @@ from pyfibaro.fibaro_state_resolver import FibaroEvent, FibaroStateResolver
from requests.exceptions import HTTPError
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ARMED,
ATTR_BATTERY_LEVEL,
CONF_PASSWORD,
CONF_URL,
CONF_USERNAME,
Platform,
)
from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import (
ConfigEntryAuthFailed,
@ -31,7 +24,6 @@ from homeassistant.exceptions import (
)
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceEntry, DeviceInfo
from homeassistant.helpers.entity import Entity
from homeassistant.util import slugify
from .const import CONF_IMPORT_PLUGINS, DOMAIN
@ -450,118 +442,6 @@ async def async_remove_config_entry_device(
return True
class FibaroDevice(Entity):
"""Representation of a Fibaro device entity."""
_attr_should_poll = False
def __init__(self, fibaro_device: DeviceModel) -> None:
"""Initialize the device."""
self.fibaro_device = fibaro_device
self.controller = fibaro_device.fibaro_controller
self.ha_id = fibaro_device.ha_id
self._attr_name = fibaro_device.friendly_name
self._attr_unique_id = fibaro_device.unique_id_str
self._attr_device_info = self.controller.get_device_info(fibaro_device)
# propagate hidden attribute set in fibaro home center to HA
if not fibaro_device.visible:
self._attr_entity_registry_visible_default = False
async def async_added_to_hass(self) -> None:
"""Call when entity is added to hass."""
self.controller.register(self.fibaro_device.fibaro_id, self._update_callback)
def _update_callback(self) -> None:
"""Update the state."""
self.schedule_update_ha_state(True)
@property
def level(self) -> int | None:
"""Get the level of Fibaro device."""
if self.fibaro_device.value.has_value:
return self.fibaro_device.value.int_value()
return None
@property
def level2(self) -> int | None:
"""Get the tilt level of Fibaro device."""
if self.fibaro_device.value_2.has_value:
return self.fibaro_device.value_2.int_value()
return None
def dont_know_message(self, cmd: str) -> None:
"""Make a warning in case we don't know how to perform an action."""
_LOGGER.warning(
"Not sure how to %s: %s (available actions: %s)",
cmd,
str(self.ha_id),
str(self.fibaro_device.actions),
)
def set_level(self, level: int) -> None:
"""Set the level of Fibaro device."""
self.action("setValue", level)
if self.fibaro_device.value.has_value:
self.fibaro_device.properties["value"] = level
if self.fibaro_device.has_brightness:
self.fibaro_device.properties["brightness"] = level
def set_level2(self, level: int) -> None:
"""Set the level2 of Fibaro device."""
self.action("setValue2", level)
if self.fibaro_device.value_2.has_value:
self.fibaro_device.properties["value2"] = level
def call_turn_on(self) -> None:
"""Turn on the Fibaro device."""
self.action("turnOn")
def call_turn_off(self) -> None:
"""Turn off the Fibaro device."""
self.action("turnOff")
def call_set_color(self, red: int, green: int, blue: int, white: int) -> None:
"""Set the color of Fibaro device."""
red = int(max(0, min(255, red)))
green = int(max(0, min(255, green)))
blue = int(max(0, min(255, blue)))
white = int(max(0, min(255, white)))
color_str = f"{red},{green},{blue},{white}"
self.fibaro_device.properties["color"] = color_str
self.action("setColor", str(red), str(green), str(blue), str(white))
def action(self, cmd: str, *args: Any) -> None:
"""Perform an action on the Fibaro HC."""
if cmd in self.fibaro_device.actions:
self.fibaro_device.execute_action(cmd, args)
_LOGGER.debug("-> %s.%s%s called", str(self.ha_id), str(cmd), str(args))
else:
self.dont_know_message(cmd)
@property
def current_binary_state(self) -> bool:
"""Return the current binary state."""
return self.fibaro_device.value.bool_value(False)
@property
def extra_state_attributes(self) -> Mapping[str, Any]:
"""Return the state attributes of the device."""
attr = {"fibaro_id": self.fibaro_device.fibaro_id}
if self.fibaro_device.has_battery_level:
attr[ATTR_BATTERY_LEVEL] = self.fibaro_device.battery_level
if self.fibaro_device.has_armed:
attr[ATTR_ARMED] = self.fibaro_device.armed
return attr
def update(self) -> None:
"""Update the available state of the entity."""
if self.fibaro_device.has_dead:
self._attr_available = not self.fibaro_device.dead
class FibaroConnectFailed(HomeAssistantError):
"""Error to indicate we cannot connect to fibaro home center."""

View file

@ -17,8 +17,9 @@ from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import FibaroController, FibaroDevice
from . import FibaroController
from .const import DOMAIN
from .entity import FibaroEntity
SENSOR_TYPES = {
"com.fibaro.floodSensor": ["Flood", "mdi:water", BinarySensorDeviceClass.MOISTURE],
@ -56,7 +57,7 @@ async def async_setup_entry(
)
class FibaroBinarySensor(FibaroDevice, BinarySensorEntity):
class FibaroBinarySensor(FibaroEntity, BinarySensorEntity):
"""Representation of a Fibaro Binary Sensor."""
def __init__(self, fibaro_device: DeviceModel) -> None:

View file

@ -22,8 +22,9 @@ from homeassistant.const import ATTR_TEMPERATURE, Platform, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import FibaroController, FibaroDevice
from . import FibaroController
from .const import DOMAIN
from .entity import FibaroEntity
PRESET_RESUME = "resume"
PRESET_MOIST = "moist"
@ -124,7 +125,7 @@ async def async_setup_entry(
)
class FibaroThermostat(FibaroDevice, ClimateEntity):
class FibaroThermostat(FibaroEntity, ClimateEntity):
"""Representation of a Fibaro Thermostat."""
_enable_turn_on_off_backwards_compatibility = False
@ -132,10 +133,10 @@ class FibaroThermostat(FibaroDevice, ClimateEntity):
def __init__(self, fibaro_device: DeviceModel) -> None:
"""Initialize the Fibaro device."""
super().__init__(fibaro_device)
self._temp_sensor_device: FibaroDevice | None = None
self._target_temp_device: FibaroDevice | None = None
self._op_mode_device: FibaroDevice | None = None
self._fan_mode_device: FibaroDevice | None = None
self._temp_sensor_device: FibaroEntity | None = None
self._target_temp_device: FibaroEntity | None = None
self._op_mode_device: FibaroEntity | None = None
self._fan_mode_device: FibaroEntity | None = None
self.entity_id = ENTITY_ID_FORMAT.format(self.ha_id)
siblings = fibaro_device.fibaro_controller.get_siblings(fibaro_device)
@ -150,23 +151,23 @@ class FibaroThermostat(FibaroDevice, ClimateEntity):
and (device.value.has_value or device.has_heating_thermostat_setpoint)
and device.unit in ("C", "F")
):
self._temp_sensor_device = FibaroDevice(device)
self._temp_sensor_device = FibaroEntity(device)
tempunit = device.unit
if any(
action for action in TARGET_TEMP_ACTIONS if action in device.actions
):
self._target_temp_device = FibaroDevice(device)
self._target_temp_device = FibaroEntity(device)
self._attr_supported_features |= ClimateEntityFeature.TARGET_TEMPERATURE
if device.has_unit:
tempunit = device.unit
if any(action for action in OP_MODE_ACTIONS if action in device.actions):
self._op_mode_device = FibaroDevice(device)
self._op_mode_device = FibaroEntity(device)
self._attr_supported_features |= ClimateEntityFeature.PRESET_MODE
if "setFanMode" in device.actions:
self._fan_mode_device = FibaroDevice(device)
self._fan_mode_device = FibaroEntity(device)
self._attr_supported_features |= ClimateEntityFeature.FAN_MODE
if tempunit == "F":

View file

@ -18,8 +18,9 @@ from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import FibaroController, FibaroDevice
from . import FibaroController
from .const import DOMAIN
from .entity import FibaroEntity
async def async_setup_entry(
@ -35,7 +36,7 @@ async def async_setup_entry(
)
class FibaroCover(FibaroDevice, CoverEntity):
class FibaroCover(FibaroEntity, CoverEntity):
"""Representation a Fibaro Cover."""
def __init__(self, fibaro_device: DeviceModel) -> None:

View file

@ -0,0 +1,126 @@
"""Support for the Fibaro devices."""
from __future__ import annotations
from collections.abc import Mapping
import logging
from typing import Any
from pyfibaro.fibaro_device import DeviceModel
from homeassistant.const import ATTR_ARMED, ATTR_BATTERY_LEVEL
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
class FibaroEntity(Entity):
"""Representation of a Fibaro device entity."""
_attr_should_poll = False
def __init__(self, fibaro_device: DeviceModel) -> None:
"""Initialize the device."""
self.fibaro_device = fibaro_device
self.controller = fibaro_device.fibaro_controller
self.ha_id = fibaro_device.ha_id
self._attr_name = fibaro_device.friendly_name
self._attr_unique_id = fibaro_device.unique_id_str
self._attr_device_info = self.controller.get_device_info(fibaro_device)
# propagate hidden attribute set in fibaro home center to HA
if not fibaro_device.visible:
self._attr_entity_registry_visible_default = False
async def async_added_to_hass(self) -> None:
"""Call when entity is added to hass."""
self.controller.register(self.fibaro_device.fibaro_id, self._update_callback)
def _update_callback(self) -> None:
"""Update the state."""
self.schedule_update_ha_state(True)
@property
def level(self) -> int | None:
"""Get the level of Fibaro device."""
if self.fibaro_device.value.has_value:
return self.fibaro_device.value.int_value()
return None
@property
def level2(self) -> int | None:
"""Get the tilt level of Fibaro device."""
if self.fibaro_device.value_2.has_value:
return self.fibaro_device.value_2.int_value()
return None
def dont_know_message(self, cmd: str) -> None:
"""Make a warning in case we don't know how to perform an action."""
_LOGGER.warning(
"Not sure how to %s: %s (available actions: %s)",
cmd,
str(self.ha_id),
str(self.fibaro_device.actions),
)
def set_level(self, level: int) -> None:
"""Set the level of Fibaro device."""
self.action("setValue", level)
if self.fibaro_device.value.has_value:
self.fibaro_device.properties["value"] = level
if self.fibaro_device.has_brightness:
self.fibaro_device.properties["brightness"] = level
def set_level2(self, level: int) -> None:
"""Set the level2 of Fibaro device."""
self.action("setValue2", level)
if self.fibaro_device.value_2.has_value:
self.fibaro_device.properties["value2"] = level
def call_turn_on(self) -> None:
"""Turn on the Fibaro device."""
self.action("turnOn")
def call_turn_off(self) -> None:
"""Turn off the Fibaro device."""
self.action("turnOff")
def call_set_color(self, red: int, green: int, blue: int, white: int) -> None:
"""Set the color of Fibaro device."""
red = int(max(0, min(255, red)))
green = int(max(0, min(255, green)))
blue = int(max(0, min(255, blue)))
white = int(max(0, min(255, white)))
color_str = f"{red},{green},{blue},{white}"
self.fibaro_device.properties["color"] = color_str
self.action("setColor", str(red), str(green), str(blue), str(white))
def action(self, cmd: str, *args: Any) -> None:
"""Perform an action on the Fibaro HC."""
if cmd in self.fibaro_device.actions:
self.fibaro_device.execute_action(cmd, args)
_LOGGER.debug("-> %s.%s%s called", str(self.ha_id), str(cmd), str(args))
else:
self.dont_know_message(cmd)
@property
def current_binary_state(self) -> bool:
"""Return the current binary state."""
return self.fibaro_device.value.bool_value(False)
@property
def extra_state_attributes(self) -> Mapping[str, Any]:
"""Return the state attributes of the device."""
attr = {"fibaro_id": self.fibaro_device.fibaro_id}
if self.fibaro_device.has_battery_level:
attr[ATTR_BATTERY_LEVEL] = self.fibaro_device.battery_level
if self.fibaro_device.has_armed:
attr[ATTR_ARMED] = self.fibaro_device.armed
return attr
def update(self) -> None:
"""Update the available state of the entity."""
if self.fibaro_device.has_dead:
self._attr_available = not self.fibaro_device.dead

View file

@ -15,8 +15,9 @@ from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import FibaroController, FibaroDevice
from . import FibaroController
from .const import DOMAIN
from .entity import FibaroEntity
async def async_setup_entry(
@ -38,7 +39,7 @@ async def async_setup_entry(
)
class FibaroEventEntity(FibaroDevice, EventEntity):
class FibaroEventEntity(FibaroEntity, EventEntity):
"""Representation of a Fibaro Event Entity."""
def __init__(self, fibaro_device: DeviceModel, scene_event: SceneEvent) -> None:

View file

@ -22,8 +22,9 @@ from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import FibaroController, FibaroDevice
from . import FibaroController
from .const import DOMAIN
from .entity import FibaroEntity
PARALLEL_UPDATES = 2
@ -62,7 +63,7 @@ async def async_setup_entry(
)
class FibaroLight(FibaroDevice, LightEntity):
class FibaroLight(FibaroEntity, LightEntity):
"""Representation of a Fibaro Light, including dimmable."""
def __init__(self, fibaro_device: DeviceModel) -> None:

View file

@ -12,8 +12,9 @@ from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import FibaroController, FibaroDevice
from . import FibaroController
from .const import DOMAIN
from .entity import FibaroEntity
async def async_setup_entry(
@ -29,7 +30,7 @@ async def async_setup_entry(
)
class FibaroLock(FibaroDevice, LockEntity):
class FibaroLock(FibaroEntity, LockEntity):
"""Representation of a Fibaro Lock."""
def __init__(self, fibaro_device: DeviceModel) -> None:

View file

@ -27,8 +27,9 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import convert
from . import FibaroController, FibaroDevice
from . import FibaroController
from .const import DOMAIN
from .entity import FibaroEntity
# List of known sensors which represents a fibaro device
MAIN_SENSOR_TYPES: dict[str, SensorEntityDescription] = {
@ -132,7 +133,7 @@ async def async_setup_entry(
async_add_entities(entities, True)
class FibaroSensor(FibaroDevice, SensorEntity):
class FibaroSensor(FibaroEntity, SensorEntity):
"""Representation of a Fibaro Sensor."""
def __init__(
@ -161,7 +162,7 @@ class FibaroSensor(FibaroDevice, SensorEntity):
self._attr_native_value = self.fibaro_device.value.float_value()
class FibaroAdditionalSensor(FibaroDevice, SensorEntity):
class FibaroAdditionalSensor(FibaroEntity, SensorEntity):
"""Representation of a Fibaro Additional Sensor."""
def __init__(

View file

@ -12,8 +12,9 @@ from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import FibaroController, FibaroDevice
from . import FibaroController
from .const import DOMAIN
from .entity import FibaroEntity
async def async_setup_entry(
@ -29,7 +30,7 @@ async def async_setup_entry(
)
class FibaroSwitch(FibaroDevice, SwitchEntity):
class FibaroSwitch(FibaroEntity, SwitchEntity):
"""Representation of a Fibaro Switch."""
def __init__(self, fibaro_device: DeviceModel) -> None: