Fix accelerator sensor in fibaro integration (#81237)
* Fix accelerator sensor in fibaro integration * Implement suggestions from code review * Implement suggestions from code review * Changes as suggested in code review * Adjust as suggested in code reviewpull/82197/head
parent
d94e969dc1
commit
7d20bb0532
|
@ -84,6 +84,7 @@ FIBARO_TYPEMAP = {
|
|||
"com.fibaro.thermostatDanfoss": Platform.CLIMATE,
|
||||
"com.fibaro.doorLock": Platform.LOCK,
|
||||
"com.fibaro.binarySensor": Platform.BINARY_SENSOR,
|
||||
"com.fibaro.accelerometer": Platform.BINARY_SENSOR,
|
||||
}
|
||||
|
||||
DEVICE_CONFIG_SCHEMA_ENTRY = vol.Schema(
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
"""Support for Fibaro binary sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
|
@ -28,6 +30,11 @@ SENSOR_TYPES = {
|
|||
"com.fibaro.smokeSensor": ["Smoke", "mdi:smoking", BinarySensorDeviceClass.SMOKE],
|
||||
"com.fibaro.FGMS001": ["Motion", "mdi:run", BinarySensorDeviceClass.MOTION],
|
||||
"com.fibaro.heatDetector": ["Heat", "mdi:fire", BinarySensorDeviceClass.HEAT],
|
||||
"com.fibaro.accelerometer": [
|
||||
"Moving",
|
||||
"mdi:axis-arrow",
|
||||
BinarySensorDeviceClass.MOVING,
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
|
@ -55,15 +62,50 @@ class FibaroBinarySensor(FibaroDevice, BinarySensorEntity):
|
|||
"""Initialize the binary_sensor."""
|
||||
super().__init__(fibaro_device)
|
||||
self.entity_id = ENTITY_ID_FORMAT.format(self.ha_id)
|
||||
stype = None
|
||||
self._own_extra_state_attributes: Mapping[str, Any] = {}
|
||||
self._fibaro_sensor_type = None
|
||||
if fibaro_device.type in SENSOR_TYPES:
|
||||
stype = fibaro_device.type
|
||||
self._fibaro_sensor_type = fibaro_device.type
|
||||
elif fibaro_device.baseType in SENSOR_TYPES:
|
||||
stype = fibaro_device.baseType
|
||||
if stype:
|
||||
self._attr_device_class = SENSOR_TYPES[stype][2]
|
||||
self._attr_icon = SENSOR_TYPES[stype][1]
|
||||
self._fibaro_sensor_type = fibaro_device.baseType
|
||||
if self._fibaro_sensor_type:
|
||||
self._attr_device_class = SENSOR_TYPES[self._fibaro_sensor_type][2]
|
||||
self._attr_icon = SENSOR_TYPES[self._fibaro_sensor_type][1]
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
||||
"""Return the extra state attributes of the device."""
|
||||
return super().extra_state_attributes | self._own_extra_state_attributes
|
||||
|
||||
def update(self) -> None:
|
||||
"""Get the latest data and update the state."""
|
||||
self._attr_is_on = self.current_binary_state
|
||||
if self._fibaro_sensor_type == "com.fibaro.accelerometer":
|
||||
# Accelerator sensors have values for the three axis x, y and z
|
||||
moving_values = self._get_moving_values()
|
||||
self._attr_is_on = self._is_moving(moving_values)
|
||||
self._own_extra_state_attributes = self._get_xyz_moving(moving_values)
|
||||
else:
|
||||
self._attr_is_on = self.current_binary_state
|
||||
|
||||
def _get_xyz_moving(self, moving_values: Mapping[str, Any]) -> Mapping[str, Any]:
|
||||
"""Return x y z values of the accelerator sensor value."""
|
||||
attrs = {}
|
||||
for axis_name in ("x", "y", "z"):
|
||||
attrs[axis_name] = float(moving_values[axis_name])
|
||||
return attrs
|
||||
|
||||
def _is_moving(self, moving_values: Mapping[str, Any]) -> bool:
|
||||
"""Return that a moving is detected when one axis reports a value."""
|
||||
for axis_name in ("x", "y", "z"):
|
||||
if float(moving_values[axis_name]) != 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
def _get_moving_values(self) -> Mapping[str, Any]:
|
||||
"""Get the moving values of the accelerator sensor in a dict."""
|
||||
value = self.fibaro_device.properties.value
|
||||
if isinstance(value, str):
|
||||
# HC2 returns dict as str
|
||||
return json.loads(value)
|
||||
# HC3 returns a real dict
|
||||
return value
|
||||
|
|
Loading…
Reference in New Issue