Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix accelerator sensor in fibaro integration #81237

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions homeassistant/components/fibaro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"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(
Expand Down
44 changes: 43 additions & 1 deletion homeassistant/components/fibaro/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -28,6 +30,11 @@
"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,
],
}


Expand Down Expand Up @@ -55,6 +62,7 @@ def __init__(self, fibaro_device: Any) -> None:
"""Initialize the binary_sensor."""
super().__init__(fibaro_device)
self.entity_id = ENTITY_ID_FORMAT.format(self.ha_id)
self._own_extra_state_attributes: Mapping[str, Any] = {}
stype = None
if fibaro_device.type in SENSOR_TYPES:
stype = fibaro_device.type
Expand All @@ -64,6 +72,40 @@ def __init__(self, fibaro_device: Any) -> None:
self._attr_device_class = SENSOR_TYPES[stype][2]
self._attr_icon = SENSOR_TYPES[stype][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.device_class == BinarySensorDeviceClass.MOVING:
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
# 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