mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
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:
parent
2deab9e0c2
commit
0672e1a1ea
@ -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):
|
||||
|
@ -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(
|
||||
|
@ -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."""
|
||||
|
39
tests/components/fibaro/test_sensor.py
Normal file
39
tests/components/fibaro/test_sensor.py
Normal 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
|
Loading…
x
Reference in New Issue
Block a user