diff --git a/homeassistant/components/homewizard/sensor.py b/homeassistant/components/homewizard/sensor.py index afe2917ad27..4441d50061e 100644 --- a/homeassistant/components/homewizard/sensor.py +++ b/homeassistant/components/homewizard/sensor.py @@ -74,7 +74,11 @@ SENSORS: Final[tuple[HomeWizardSensorEntityDescription, ...]] = ( key="active_tariff", name="Active tariff", icon="mdi:calendar-clock", - value_fn=lambda data: data.active_tariff, + value_fn=lambda data: ( + None if data.active_tariff is None else str(data.active_tariff) + ), + device_class=SensorDeviceClass.ENUM, + options=["1", "2", "3", "4"], ), HomeWizardSensorEntityDescription( key="wifi_strength", diff --git a/tests/components/homewizard/test_sensor.py b/tests/components/homewizard/test_sensor.py index c90c11ac693..445e1aeab45 100644 --- a/tests/components/homewizard/test_sensor.py +++ b/tests/components/homewizard/test_sensor.py @@ -7,6 +7,7 @@ from homewizard_energy.errors import DisabledError, RequestError from homewizard_energy.models import Data from homeassistant.components.sensor import ( + ATTR_OPTIONS, ATTR_STATE_CLASS, SensorDeviceClass, SensorStateClass, @@ -145,6 +146,45 @@ async def test_sensor_entity_wifi_ssid(hass, mock_config_entry_data, mock_config assert state.attributes.get(ATTR_ICON) == "mdi:wifi" +async def test_sensor_entity_active_tariff( + hass, mock_config_entry_data, mock_config_entry +): + """Test entity loads active_tariff.""" + + api = get_mock_device() + api.data = AsyncMock(return_value=Data.from_dict({"active_tariff": 2})) + + with patch( + "homeassistant.components.homewizard.coordinator.HomeWizardEnergy", + return_value=api, + ): + entry = mock_config_entry + entry.data = mock_config_entry_data + entry.add_to_hass(hass) + + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + entity_registry = er.async_get(hass) + + state = hass.states.get("sensor.product_name_aabbccddeeff_active_tariff") + entry = entity_registry.async_get("sensor.product_name_aabbccddeeff_active_tariff") + assert entry + assert state + assert entry.unique_id == "aabbccddeeff_active_tariff" + assert not entry.disabled + assert state.state == "2" + assert ( + state.attributes.get(ATTR_FRIENDLY_NAME) + == "Product Name (aabbccddeeff) Active tariff" + ) + assert ATTR_STATE_CLASS not in state.attributes + assert ATTR_UNIT_OF_MEASUREMENT not in state.attributes + assert state.attributes.get(ATTR_ICON) == "mdi:calendar-clock" + assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENUM + assert state.attributes.get(ATTR_OPTIONS) == ["1", "2", "3", "4"] + + async def test_sensor_entity_wifi_strength( hass, mock_config_entry_data, mock_config_entry ):