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 Homee brightness sensors reporting in percent #139409

Merged
merged 12 commits into from
Mar 4, 2025
6 changes: 6 additions & 0 deletions homeassistant/components/homee/icons.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"entity": {
"sensor": {
"brightness": {
"default": "mdi:brightness-5"
},
"brightness_instance": {
"default": "mdi:brightness-5"
},
"link_quality": {
"default": "mdi:signal"
},
Expand Down
16 changes: 16 additions & 0 deletions homeassistant/components/homee/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,22 @@ def get_window_value(attribute: HomeeAttribute) -> str | None:
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)
class HomeeSensorEntityDescription(SensorEntityDescription):
"""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] = (
lambda value: value.current_value
)
Expand All @@ -67,6 +79,7 @@ class HomeeSensorEntityDescription(SensorEntityDescription):
AttributeType.BRIGHTNESS: HomeeSensorEntityDescription(
key="brightness",
device_class=SensorDeviceClass.ILLUMINANCE,
device_class_fn=get_brightness_device_class,
state_class=SensorStateClass.MEASUREMENT,
value_fn=(
lambda attribute: attribute.current_value * 1000
Expand Down Expand Up @@ -303,6 +316,9 @@ def __init__(
if attribute.instance > 0:
self._attr_translation_key = f"{self._attr_translation_key}_instance"
self._attr_translation_placeholders = {"instance": str(attribute.instance)}
self._attr_device_class = description.device_class_fn(
attribute, description.device_class
)

@property
def native_value(self) -> float | str | None:
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/homee/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@
}
},
"sensor": {
"brightness": {
"name": "Illuminance"
},
"brightness_instance": {
"name": "Illuminance {instance}"
},
Expand Down
23 changes: 22 additions & 1 deletion tests/components/homee/fixtures/sensors.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@
"data": "",
"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,
"node_id": 1,
Expand All @@ -93,7 +114,7 @@
"unit": "%",
"step_value": 1.0,
"editable": 0,
"type": 8,
"type": 11,
"state": 1,
"last_changed": 1709982926,
"changed_by": 1,
Expand Down
55 changes: 53 additions & 2 deletions tests/components/homee/snapshots/test_sensor.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@
'platform': 'homee',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'battery',
'unique_id': '00055511EECC-1-4',
'translation_key': 'battery_instance',
'unique_id': '00055511EECC-1-34',
'unit_of_measurement': '%',
})
# ---
Expand Down Expand Up @@ -518,6 +518,57 @@
'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]
EntityRegistryEntrySnapshot({
'aliases': set({
Expand Down
28 changes: 3 additions & 25 deletions tests/components/homee/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
WINDOW_MAP,
WINDOW_MAP_REVERSED,
)
from homeassistant.const import LIGHT_LUX, Platform
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er

Expand All @@ -37,7 +37,7 @@ async def test_up_down_values(

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):
await async_update_attribute_value(hass, attribute, i)
assert (
Expand Down Expand Up @@ -69,7 +69,7 @@ async def test_window_position(
== WINDOW_MAP[0]
)

attribute = mock_homee.nodes[0].attributes[32]
attribute = mock_homee.nodes[0].attributes[33]
for i in range(1, 3):
await async_update_attribute_value(hass, attribute, i)
assert (
Expand All @@ -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(
hass: HomeAssistant,
mock_homee: MagicMock,
Expand Down