mirror of
https://github.com/home-assistant/core.git
synced 2025-06-25 01:21:51 +02:00
Fix Homee brightness sensors reporting in percent (#139409)
* fix brigtness sensor having percent as unit. * add test for percent-brightness-sensor * remove valve position and update tests * Removed test, because covered by Snapshots * fix review comments * move device calss to init. * fix test * fix review comments * add battery sensor back to test fixture * fix
This commit is contained in:
@ -1,6 +1,12 @@
|
|||||||
{
|
{
|
||||||
"entity": {
|
"entity": {
|
||||||
"sensor": {
|
"sensor": {
|
||||||
|
"brightness": {
|
||||||
|
"default": "mdi:brightness-5"
|
||||||
|
},
|
||||||
|
"brightness_instance": {
|
||||||
|
"default": "mdi:brightness-5"
|
||||||
|
},
|
||||||
"link_quality": {
|
"link_quality": {
|
||||||
"default": "mdi:signal"
|
"default": "mdi:signal"
|
||||||
},
|
},
|
||||||
|
@ -40,10 +40,22 @@ def get_window_value(attribute: HomeeAttribute) -> str | None:
|
|||||||
return vals.get(attribute.current_value)
|
return vals.get(attribute.current_value)
|
||||||
|
|
||||||
|
|
||||||
|
def get_brightness_device_class(
|
||||||
|
attribute: HomeeAttribute, device_class: SensorDeviceClass | None
|
||||||
|
) -> SensorDeviceClass | None:
|
||||||
|
"""Return the device class for a brightness sensor."""
|
||||||
|
if attribute.unit == "%":
|
||||||
|
return None
|
||||||
|
return device_class
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class HomeeSensorEntityDescription(SensorEntityDescription):
|
class HomeeSensorEntityDescription(SensorEntityDescription):
|
||||||
"""A class that describes Homee sensor entities."""
|
"""A class that describes Homee sensor entities."""
|
||||||
|
|
||||||
|
device_class_fn: Callable[
|
||||||
|
[HomeeAttribute, SensorDeviceClass | None], SensorDeviceClass | None
|
||||||
|
] = lambda attribute, device_class: device_class
|
||||||
value_fn: Callable[[HomeeAttribute], str | float | None] = (
|
value_fn: Callable[[HomeeAttribute], str | float | None] = (
|
||||||
lambda value: value.current_value
|
lambda value: value.current_value
|
||||||
)
|
)
|
||||||
@ -67,6 +79,7 @@ SENSOR_DESCRIPTIONS: dict[AttributeType, HomeeSensorEntityDescription] = {
|
|||||||
AttributeType.BRIGHTNESS: HomeeSensorEntityDescription(
|
AttributeType.BRIGHTNESS: HomeeSensorEntityDescription(
|
||||||
key="brightness",
|
key="brightness",
|
||||||
device_class=SensorDeviceClass.ILLUMINANCE,
|
device_class=SensorDeviceClass.ILLUMINANCE,
|
||||||
|
device_class_fn=get_brightness_device_class,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
value_fn=(
|
value_fn=(
|
||||||
lambda attribute: attribute.current_value * 1000
|
lambda attribute: attribute.current_value * 1000
|
||||||
@ -303,6 +316,9 @@ class HomeeSensor(HomeeEntity, SensorEntity):
|
|||||||
if attribute.instance > 0:
|
if attribute.instance > 0:
|
||||||
self._attr_translation_key = f"{self._attr_translation_key}_instance"
|
self._attr_translation_key = f"{self._attr_translation_key}_instance"
|
||||||
self._attr_translation_placeholders = {"instance": str(attribute.instance)}
|
self._attr_translation_placeholders = {"instance": str(attribute.instance)}
|
||||||
|
self._attr_device_class = description.device_class_fn(
|
||||||
|
attribute, description.device_class
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> float | str | None:
|
def native_value(self) -> float | str | None:
|
||||||
|
@ -111,6 +111,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"sensor": {
|
"sensor": {
|
||||||
|
"brightness": {
|
||||||
|
"name": "Illuminance"
|
||||||
|
},
|
||||||
"brightness_instance": {
|
"brightness_instance": {
|
||||||
"name": "Illuminance {instance}"
|
"name": "Illuminance {instance}"
|
||||||
},
|
},
|
||||||
|
@ -81,6 +81,27 @@
|
|||||||
"data": "",
|
"data": "",
|
||||||
"name": ""
|
"name": ""
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"id": 34,
|
||||||
|
"node_id": 1,
|
||||||
|
"instance": 2,
|
||||||
|
"minimum": 0,
|
||||||
|
"maximum": 100,
|
||||||
|
"current_value": 100.0,
|
||||||
|
"target_value": 100.0,
|
||||||
|
"last_value": 100.0,
|
||||||
|
"unit": "%",
|
||||||
|
"step_value": 1.0,
|
||||||
|
"editable": 0,
|
||||||
|
"type": 8,
|
||||||
|
"state": 1,
|
||||||
|
"last_changed": 1709982926,
|
||||||
|
"changed_by": 1,
|
||||||
|
"changed_by_id": 0,
|
||||||
|
"based_on": 1,
|
||||||
|
"data": "",
|
||||||
|
"name": ""
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"id": 4,
|
"id": 4,
|
||||||
"node_id": 1,
|
"node_id": 1,
|
||||||
@ -93,7 +114,7 @@
|
|||||||
"unit": "%",
|
"unit": "%",
|
||||||
"step_value": 1.0,
|
"step_value": 1.0,
|
||||||
"editable": 0,
|
"editable": 0,
|
||||||
"type": 8,
|
"type": 11,
|
||||||
"state": 1,
|
"state": 1,
|
||||||
"last_changed": 1709982926,
|
"last_changed": 1709982926,
|
||||||
"changed_by": 1,
|
"changed_by": 1,
|
||||||
|
@ -82,8 +82,8 @@
|
|||||||
'platform': 'homee',
|
'platform': 'homee',
|
||||||
'previous_unique_id': None,
|
'previous_unique_id': None,
|
||||||
'supported_features': 0,
|
'supported_features': 0,
|
||||||
'translation_key': 'battery',
|
'translation_key': 'battery_instance',
|
||||||
'unique_id': '00055511EECC-1-4',
|
'unique_id': '00055511EECC-1-34',
|
||||||
'unit_of_measurement': '%',
|
'unit_of_measurement': '%',
|
||||||
})
|
})
|
||||||
# ---
|
# ---
|
||||||
@ -518,6 +518,57 @@
|
|||||||
'state': '51.0',
|
'state': '51.0',
|
||||||
})
|
})
|
||||||
# ---
|
# ---
|
||||||
|
# name: test_sensor_snapshot[sensor.test_multisensor_illuminance-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': dict({
|
||||||
|
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||||
|
}),
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'config_subentry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'sensor',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'sensor.test_multisensor_illuminance',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': None,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Illuminance',
|
||||||
|
'platform': 'homee',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'brightness',
|
||||||
|
'unique_id': '00055511EECC-1-4',
|
||||||
|
'unit_of_measurement': '%',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_sensor_snapshot[sensor.test_multisensor_illuminance-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'friendly_name': 'Test MultiSensor Illuminance',
|
||||||
|
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||||
|
'unit_of_measurement': '%',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'sensor.test_multisensor_illuminance',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': '100.0',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
# name: test_sensor_snapshot[sensor.test_multisensor_illuminance_1-entry]
|
# name: test_sensor_snapshot[sensor.test_multisensor_illuminance_1-entry]
|
||||||
EntityRegistryEntrySnapshot({
|
EntityRegistryEntrySnapshot({
|
||||||
'aliases': set({
|
'aliases': set({
|
||||||
|
@ -11,7 +11,7 @@ from homeassistant.components.homee.const import (
|
|||||||
WINDOW_MAP,
|
WINDOW_MAP,
|
||||||
WINDOW_MAP_REVERSED,
|
WINDOW_MAP_REVERSED,
|
||||||
)
|
)
|
||||||
from homeassistant.const import LIGHT_LUX, Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ async def test_up_down_values(
|
|||||||
|
|
||||||
assert hass.states.get("sensor.test_multisensor_state").state == OPEN_CLOSE_MAP[0]
|
assert hass.states.get("sensor.test_multisensor_state").state == OPEN_CLOSE_MAP[0]
|
||||||
|
|
||||||
attribute = mock_homee.nodes[0].attributes[27]
|
attribute = mock_homee.nodes[0].attributes[28]
|
||||||
for i in range(1, 5):
|
for i in range(1, 5):
|
||||||
await async_update_attribute_value(hass, attribute, i)
|
await async_update_attribute_value(hass, attribute, i)
|
||||||
assert (
|
assert (
|
||||||
@ -69,7 +69,7 @@ async def test_window_position(
|
|||||||
== WINDOW_MAP[0]
|
== WINDOW_MAP[0]
|
||||||
)
|
)
|
||||||
|
|
||||||
attribute = mock_homee.nodes[0].attributes[32]
|
attribute = mock_homee.nodes[0].attributes[33]
|
||||||
for i in range(1, 3):
|
for i in range(1, 3):
|
||||||
await async_update_attribute_value(hass, attribute, i)
|
await async_update_attribute_value(hass, attribute, i)
|
||||||
assert (
|
assert (
|
||||||
@ -87,28 +87,6 @@ async def test_window_position(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_brightness_sensor(
|
|
||||||
hass: HomeAssistant,
|
|
||||||
mock_homee: MagicMock,
|
|
||||||
mock_config_entry: MockConfigEntry,
|
|
||||||
) -> None:
|
|
||||||
"""Test brightness sensor's lx & klx units and naming of multi-instance sensors."""
|
|
||||||
mock_homee.nodes = [build_mock_node("sensors.json")]
|
|
||||||
mock_homee.get_node_by_id.return_value = mock_homee.nodes[0]
|
|
||||||
await setup_integration(hass, mock_config_entry)
|
|
||||||
|
|
||||||
sensor_state = hass.states.get("sensor.test_multisensor_illuminance_1")
|
|
||||||
assert sensor_state.state == "175.0"
|
|
||||||
assert sensor_state.attributes["unit_of_measurement"] == LIGHT_LUX
|
|
||||||
assert sensor_state.attributes["friendly_name"] == "Test MultiSensor Illuminance 1"
|
|
||||||
|
|
||||||
# Sensor with Homee unit klx
|
|
||||||
sensor_state = hass.states.get("sensor.test_multisensor_illuminance_2")
|
|
||||||
assert sensor_state.state == "7000.0"
|
|
||||||
assert sensor_state.attributes["unit_of_measurement"] == LIGHT_LUX
|
|
||||||
assert sensor_state.attributes["friendly_name"] == "Test MultiSensor Illuminance 2"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_sensor_snapshot(
|
async def test_sensor_snapshot(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_homee: MagicMock,
|
mock_homee: MagicMock,
|
||||||
|
Reference in New Issue
Block a user