Add power sensor detection in fibaro integration (#126964)

* Add power sensor detection in fibaro integration

* Better solution plus test

* Update homeassistant/components/fibaro/sensor.py

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
rappenze 2024-09-30 12:01:27 +02:00 committed by GitHub
parent 2deab9e0c2
commit 0672e1a1ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 79 additions and 5 deletions

View File

@ -241,11 +241,14 @@ class FibaroController:
platform = Platform.LOCK
elif device.has_central_scene_event:
platform = Platform.EVENT
elif device.value.has_value:
if device.value.is_bool_value:
platform = Platform.BINARY_SENSOR
else:
platform = Platform.SENSOR
elif device.value.has_value and device.value.is_bool_value:
platform = Platform.BINARY_SENSOR
elif (
device.value.has_value
or "power" in device.properties
or "energy" in device.properties
):
platform = Platform.SENSOR
# Switches that control lights should show up as lights
if platform == Platform.SWITCH and device.properties.get("isLight", False):

View File

@ -112,6 +112,11 @@ async def async_setup_entry(
entities: list[SensorEntity] = [
FibaroSensor(device, MAIN_SENSOR_TYPES.get(device.type))
for device in controller.fibaro_devices[Platform.SENSOR]
# Some sensor devices do not have a value but report power or energy.
# These sensors are added to the sensor list but need to be excluded
# here as the FibaroSensor expects a value. One example is the
# Qubino 3 phase power meter.
if device.value.has_value
]
entities.extend(

View File

@ -49,6 +49,33 @@ def mock_room() -> Mock:
return room
@pytest.fixture
def mock_power_sensor() -> Mock:
"""Fixture for an individual power sensor without value."""
sensor = Mock()
sensor.fibaro_id = 1
sensor.parent_fibaro_id = 0
sensor.name = "Test sensor"
sensor.room_id = 1
sensor.visible = True
sensor.enabled = True
sensor.type = "com.fibaro.powerMeter"
sensor.base_type = "com.fibaro.device"
sensor.properties = {
"zwaveCompany": "Goap",
"endPointId": "2",
"manufacturer": "",
"power": "6.60",
}
sensor.actions = {}
sensor.has_central_scene_event = False
value_mock = Mock()
value_mock.has_value = False
value_mock.is_bool_value = False
sensor.value = value_mock
return sensor
@pytest.fixture
def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
"""Return the default mocked config entry."""

View File

@ -0,0 +1,39 @@
"""Test the Fibaro sensor platform."""
from unittest.mock import Mock, patch
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from .conftest import init_integration
from tests.common import MockConfigEntry
async def test_power_sensor_detected(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_fibaro_client: Mock,
mock_config_entry: MockConfigEntry,
mock_power_sensor: Mock,
mock_room: Mock,
) -> None:
"""Test that the strange power entity is detected.
Similar to a Qubino 3-Phase power meter.
"""
# Arrange
mock_fibaro_client.read_rooms.return_value = [mock_room]
mock_fibaro_client.read_devices.return_value = [mock_power_sensor]
with patch("homeassistant.components.fibaro.PLATFORMS", [Platform.SENSOR]):
# Act
await init_integration(hass, mock_config_entry)
# Assert
entry = entity_registry.async_get("sensor.room_1_test_sensor_1_power")
assert entry
assert entry.unique_id == "hc2_111111.1_power"
assert entry.original_name == "Room 1 Test sensor Power"
assert entry.original_device_class == SensorDeviceClass.POWER