diff --git a/homeassistant/components/homee/icons.json b/homeassistant/components/homee/icons.json index 17ac0ecd1f2..b4ad8871568 100644 --- a/homeassistant/components/homee/icons.json +++ b/homeassistant/components/homee/icons.json @@ -1,6 +1,12 @@ { "entity": { "sensor": { + "brightness": { + "default": "mdi:brightness-5" + }, + "brightness_instance": { + "default": "mdi:brightness-5" + }, "link_quality": { "default": "mdi:signal" }, diff --git a/homeassistant/components/homee/sensor.py b/homeassistant/components/homee/sensor.py index 86733aae778..410f87f2168 100644 --- a/homeassistant/components/homee/sensor.py +++ b/homeassistant/components/homee/sensor.py @@ -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 ) @@ -67,6 +79,7 @@ SENSOR_DESCRIPTIONS: dict[AttributeType, HomeeSensorEntityDescription] = { 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 @@ -303,6 +316,9 @@ class HomeeSensor(HomeeEntity, SensorEntity): 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: diff --git a/homeassistant/components/homee/strings.json b/homeassistant/components/homee/strings.json index cf5b90dbe2a..94f85824280 100644 --- a/homeassistant/components/homee/strings.json +++ b/homeassistant/components/homee/strings.json @@ -111,6 +111,9 @@ } }, "sensor": { + "brightness": { + "name": "Illuminance" + }, "brightness_instance": { "name": "Illuminance {instance}" }, diff --git a/tests/components/homee/fixtures/sensors.json b/tests/components/homee/fixtures/sensors.json index f4a7f462218..bcc36a85ee7 100644 --- a/tests/components/homee/fixtures/sensors.json +++ b/tests/components/homee/fixtures/sensors.json @@ -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, @@ -93,7 +114,7 @@ "unit": "%", "step_value": 1.0, "editable": 0, - "type": 8, + "type": 11, "state": 1, "last_changed": 1709982926, "changed_by": 1, diff --git a/tests/components/homee/snapshots/test_sensor.ambr b/tests/components/homee/snapshots/test_sensor.ambr index 3101723232e..b35943630d5 100644 --- a/tests/components/homee/snapshots/test_sensor.ambr +++ b/tests/components/homee/snapshots/test_sensor.ambr @@ -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': '%', }) # --- @@ -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': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_multisensor_illuminance', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + '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': , + 'unit_of_measurement': '%', + }), + 'context': , + 'entity_id': 'sensor.test_multisensor_illuminance', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '100.0', + }) +# --- # name: test_sensor_snapshot[sensor.test_multisensor_illuminance_1-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ diff --git a/tests/components/homee/test_sensor.py b/tests/components/homee/test_sensor.py index a2ba991c49b..bbdad4c4469 100644 --- a/tests/components/homee/test_sensor.py +++ b/tests/components/homee/test_sensor.py @@ -11,7 +11,7 @@ from homeassistant.components.homee.const import ( 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 @@ -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 ( @@ -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 ( @@ -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,