diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index 6cabee6ccba..0d782f46c24 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -1082,6 +1082,7 @@ RPC_SENSORS: Final = { or config[key]["enable"] is False or status[key].get("xpercent") is None ), + unit=lambda config: config["xpercent"]["unit"] or None, ), "pulse_counter": RpcSensorDescription( key="input", @@ -1104,6 +1105,7 @@ RPC_SENSORS: Final = { or config[key]["enable"] is False or status[key]["counts"].get("xtotal") is None ), + unit=lambda config: config["xcounts"]["unit"] or None, ), "counter_frequency": RpcSensorDescription( key="input", @@ -1124,6 +1126,7 @@ RPC_SENSORS: Final = { or config[key]["enable"] is False or status[key].get("xfreq") is None ), + unit=lambda config: config["xfreq"]["unit"] or None, ), "text": RpcSensorDescription( key="text", diff --git a/tests/components/shelly/conftest.py b/tests/components/shelly/conftest.py index 34e4ce1379e..a983cbbcda9 100644 --- a/tests/components/shelly/conftest.py +++ b/tests/components/shelly/conftest.py @@ -166,8 +166,20 @@ MOCK_BLOCKS = [ MOCK_CONFIG = { "input:0": {"id": 0, "name": "Test name input 0", "type": "button"}, - "input:1": {"id": 1, "type": "analog", "enable": True}, - "input:2": {"id": 2, "name": "Gas", "type": "count", "enable": True}, + "input:1": { + "id": 1, + "type": "analog", + "enable": True, + "xpercent": {"expr": None, "unit": None}, + }, + "input:2": { + "id": 2, + "name": "Gas", + "type": "count", + "enable": True, + "xcounts": {"expr": None, "unit": None}, + "xfreq": {"expr": None, "unit": None}, + }, "light:0": {"name": "test light_0"}, "light:1": {"name": "test light_1"}, "light:2": {"name": "test light_2"}, diff --git a/tests/components/shelly/test_sensor.py b/tests/components/shelly/test_sensor.py index 6b665b8fea2..ef8a609998a 100644 --- a/tests/components/shelly/test_sensor.py +++ b/tests/components/shelly/test_sensor.py @@ -706,10 +706,27 @@ async def test_block_sleeping_update_entity_service( ) +@pytest.mark.parametrize( + ("original_unit", "expected_unit"), + [ + ("m/s", "m/s"), + (None, None), + ("", None), + ], +) async def test_rpc_analog_input_sensors( - hass: HomeAssistant, mock_rpc_device: Mock, entity_registry: EntityRegistry + hass: HomeAssistant, + mock_rpc_device: Mock, + entity_registry: EntityRegistry, + monkeypatch: pytest.MonkeyPatch, + original_unit: str | None, + expected_unit: str | None, ) -> None: """Test RPC analog input xpercent sensor.""" + config = deepcopy(mock_rpc_device.config) + config["input:1"]["xpercent"] = {"expr": "x*0.2995", "unit": original_unit} + monkeypatch.setattr(mock_rpc_device, "config", config) + await init_integration(hass, 2) entity_id = f"{SENSOR_DOMAIN}.test_name_analog_input" @@ -720,7 +737,10 @@ async def test_rpc_analog_input_sensors( assert entry.unique_id == "123456789ABC-input:1-analoginput" entity_id = f"{SENSOR_DOMAIN}.test_name_analog_value" - assert hass.states.get(entity_id).state == "8.9" + state = hass.states.get(entity_id) + assert state + assert state.state == "8.9" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == expected_unit entry = entity_registry.async_get(entity_id) assert entry @@ -764,13 +784,27 @@ async def test_rpc_disabled_xpercent( assert hass.states.get(entity_id) is None +@pytest.mark.parametrize( + ("original_unit", "expected_unit"), + [ + ("l/h", "l/h"), + (None, None), + ("", None), + ], +) async def test_rpc_pulse_counter_sensors( hass: HomeAssistant, mock_rpc_device: Mock, entity_registry: EntityRegistry, monkeypatch: pytest.MonkeyPatch, + original_unit: str | None, + expected_unit: str | None, ) -> None: """Test RPC counter sensor.""" + config = deepcopy(mock_rpc_device.config) + config["input:2"]["xcounts"] = {"expr": "x/10", "unit": original_unit} + monkeypatch.setattr(mock_rpc_device, "config", config) + await init_integration(hass, 2) entity_id = f"{SENSOR_DOMAIN}.gas_pulse_counter" @@ -784,7 +818,10 @@ async def test_rpc_pulse_counter_sensors( assert entry.unique_id == "123456789ABC-input:2-pulse_counter" entity_id = f"{SENSOR_DOMAIN}.gas_counter_value" - assert hass.states.get(entity_id).state == "561.74" + state = hass.states.get(entity_id) + assert state + assert state.state == "561.74" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == expected_unit entry = entity_registry.async_get(entity_id) assert entry @@ -828,12 +865,27 @@ async def test_rpc_disabled_xtotal_counter( assert hass.states.get(entity_id) is None +@pytest.mark.parametrize( + ("original_unit", "expected_unit"), + [ + ("W", "W"), + (None, None), + ("", None), + ], +) async def test_rpc_pulse_counter_frequency_sensors( hass: HomeAssistant, mock_rpc_device: Mock, entity_registry: EntityRegistry, + monkeypatch: pytest.MonkeyPatch, + original_unit: str | None, + expected_unit: str | None, ) -> None: """Test RPC counter sensor.""" + config = deepcopy(mock_rpc_device.config) + config["input:2"]["xfreq"] = {"expr": "x**2", "unit": original_unit} + monkeypatch.setattr(mock_rpc_device, "config", config) + await init_integration(hass, 2) entity_id = f"{SENSOR_DOMAIN}.gas_pulse_counter_frequency" @@ -847,7 +899,10 @@ async def test_rpc_pulse_counter_frequency_sensors( assert entry.unique_id == "123456789ABC-input:2-counter_frequency" entity_id = f"{SENSOR_DOMAIN}.gas_pulse_counter_frequency_value" - assert hass.states.get(entity_id).state == "6.11" + state = hass.states.get(entity_id) + assert state + assert state.state == "6.11" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == expected_unit entry = entity_registry.async_get(entity_id) assert entry