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 2 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
43 changes: 42 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 @@ -66,4 +73,38 @@ def __init__(self, fibaro_device: Any) -> None:

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
self._attr_is_on = self._is_moving()
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
else:
self._attr_is_on = self.current_binary_state

@property
def extra_state_attributes(self) -> Mapping[str, Any]:
"""Return the state attributes of the device."""
attrs = {}

# Accelerator sensors have values for the three axis x, y and z
if self.device_class == BinarySensorDeviceClass.MOVING:
moving_values = self._get_moving_values()
for axis_name in ("x", "y", "z"):
attrs[axis_name] = float(moving_values[axis_name])

return super().extra_state_attributes | attrs

def _is_moving(self) -> bool:
"""Return that a moving is detected when one axis reports a value."""
moving_values = self._get_moving_values()

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 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
16 changes: 2 additions & 14 deletions homeassistant/components/fibaro/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,8 @@ def __init__(

def update(self) -> None:
"""Update the state."""
with suppress(KeyError):
raw_value = self.fibaro_device.properties.value

if raw_value is None:
return raw_value

try:
# most sensors provide a number (HC2 as string, HC3 as float or int value)
self._attr_native_value = float(raw_value)
except (TypeError, ValueError):
# some sensors like accelerator provides a complex value,
# e.g. a accelerator provides {"x": 0.0, "y": 0.0, "z": 0.0}
# (HC2 as string HC3 as structure which is converted to RecursiveDict by the fiblary)
self._attr_native_value = str(raw_value)
with suppress(KeyError, ValueError):
self._attr_native_value = float(self.fibaro_device.properties.value)


class FibaroAdditionalSensor(FibaroDevice, SensorEntity):
Expand Down