async def test_sensor_entity_wifi_strength( hass, mock_config_entry_data, mock_config_entry ): """Test entity loads wifi strength.""" api = get_mock_device() api.data = AsyncMock(return_value=Data.from_dict({"wifi_strength": 42})) 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) entry = entity_registry.async_get("sensor.product_name_aabbccddeeff_wifi_strength") assert entry assert entry.unique_id == "aabbccddeeff_wifi_strength" assert entry.disabled
async def test_sensor_entity_total_gas(hass, mock_config_entry_data, mock_config_entry): """Test entity loads total gas.""" api = get_mock_device() api.data = AsyncMock(return_value=Data.from_dict({"total_gas_m3": 50})) 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_total_gas") entry = entity_registry.async_get("sensor.product_name_aabbccddeeff_total_gas") assert entry assert state assert entry.unique_id == "aabbccddeeff_total_gas_m3" assert not entry.disabled assert state.state == "50" assert ( state.attributes.get(ATTR_FRIENDLY_NAME) == "Product Name (aabbccddeeff) Total Gas" ) assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == VOLUME_CUBIC_METERS assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_GAS assert ATTR_ICON not in state.attributes
async def test_sensor_entity_wifi_ssid(hass, mock_config_entry_data, mock_config_entry): """Test entity loads wifi ssid.""" api = get_mock_device() api.data = AsyncMock(return_value=Data.from_dict({"wifi_ssid": "My Wifi"})) 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_wifi_ssid") entry = entity_registry.async_get("sensor.product_name_aabbccddeeff_wifi_ssid") assert entry assert state assert entry.unique_id == "aabbccddeeff_wifi_ssid" assert not entry.disabled assert state.state == "My Wifi" assert ( state.attributes.get(ATTR_FRIENDLY_NAME) == "Product Name (aabbccddeeff) Wifi SSID" ) assert ATTR_STATE_CLASS not in state.attributes assert ATTR_UNIT_OF_MEASUREMENT not in state.attributes assert ATTR_DEVICE_CLASS not in state.attributes assert state.attributes.get(ATTR_ICON) == "mdi:wifi"
async def test_api_disabled(hass, mock_config_entry_data, mock_config_entry): """Test sensor handles api unreachable.""" api = get_mock_device() api.data = AsyncMock( return_value=Data.from_dict({"total_power_import_t1_kwh": 1234.123}) ) 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() utcnow = dt_util.utcnow() # Time after the integration is setup assert ( hass.states.get( "sensor.product_name_aabbccddeeff_total_power_import_t1" ).state == "1234.123" ) api.data.side_effect = DisabledError async_fire_time_changed(hass, utcnow + timedelta(seconds=5)) await hass.async_block_till_done() assert ( hass.states.get( "sensor.product_name_aabbccddeeff_total_power_import_t1" ).state == "unavailable" ) api.data.side_effect = None async_fire_time_changed(hass, utcnow + timedelta(seconds=10)) await hass.async_block_till_done() assert ( hass.states.get( "sensor.product_name_aabbccddeeff_total_power_import_t1" ).state == "1234.123" )
async def test_sensor_entity_total_power_export_t2_kwh( hass, mock_config_entry_data, mock_config_entry ): """Test entity loads total power export t2.""" api = get_mock_device() api.data = AsyncMock( return_value=Data.from_dict({"total_power_export_t2_kwh": 1234.123}) ) 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_total_power_export_t2") entry = entity_registry.async_get( "sensor.product_name_aabbccddeeff_total_power_export_t2" ) assert entry assert state assert entry.unique_id == "aabbccddeeff_total_power_export_t2_kwh" assert not entry.disabled assert state.state == "1234.123" assert ( state.attributes.get(ATTR_FRIENDLY_NAME) == "Product Name (aabbccddeeff) Total Power Export T2" ) assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_ENERGY assert ATTR_ICON not in state.attributes
def mock_homewizardenergy(): """Return a mocked P1 meter.""" with patch( "homeassistant.components.homewizard.coordinator.HomeWizardEnergy", ) as device: client = device.return_value client.device = AsyncMock( return_value=Device.from_dict( json.loads(load_fixture("homewizard/device.json")) ) ) client.data = AsyncMock( return_value=Data.from_dict( json.loads(load_fixture("homewizard/data.json")) ) ) client.state = AsyncMock( return_value=State.from_dict( json.loads(load_fixture("homewizard/state.json")) ) ) yield device
async def test_sensor_entity_active_liters( hass, mock_config_entry_data, mock_config_entry ): """Test entity loads active liters (watermeter).""" api = get_mock_device() api.data = AsyncMock(return_value=Data.from_dict({"active_liter_lpm": 12.345})) 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_water_usage") entry = entity_registry.async_get( "sensor.product_name_aabbccddeeff_active_water_usage" ) assert entry assert state assert entry.unique_id == "aabbccddeeff_active_liter_lpm" assert not entry.disabled assert state.state == "12.345" assert ( state.attributes.get(ATTR_FRIENDLY_NAME) == "Product Name (aabbccddeeff) Active water usage" ) assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "l/min" assert ATTR_DEVICE_CLASS not in state.attributes assert state.attributes.get(ATTR_ICON) == "mdi:water"
async def test_sensor_entity_active_power_l3( hass, mock_config_entry_data, mock_config_entry ): """Test entity loads active power l3.""" api = get_mock_device() api.data = AsyncMock(return_value=Data.from_dict({"active_power_l3_w": 789.789})) 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_power_l3") entry = entity_registry.async_get( "sensor.product_name_aabbccddeeff_active_power_l3" ) assert entry assert state assert entry.unique_id == "aabbccddeeff_active_power_l3_w" assert not entry.disabled assert state.state == "789.789" assert ( state.attributes.get(ATTR_FRIENDLY_NAME) == "Product Name (aabbccddeeff) Active Power L3" ) assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_POWER assert ATTR_ICON not in state.attributes
async def test_sensor_entity_disabled_when_null( hass, mock_config_entry_data, mock_config_entry ): """Test sensor disables data with null by default.""" api = get_mock_device() api.data = AsyncMock( return_value=Data.from_dict( {"active_power_l2_w": None, "active_power_l3_w": None, "total_gas_m3": None} ) ) 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) entry = entity_registry.async_get( "sensor.product_name_aabbccddeeff_active_power_l2" ) assert entry is None entry = entity_registry.async_get( "sensor.product_name_aabbccddeeff_active_power_l3" ) assert entry is None entry = entity_registry.async_get("sensor.product_name_aabbccddeeff_total_gas") assert entry is None
async def test_sensor_entity_export_disabled_when_unused( hass, mock_config_entry_data, mock_config_entry ): """Test sensor disables export if value is 0.""" api = get_mock_device() api.data = AsyncMock( return_value=Data.from_dict( {"total_power_export_t1_kwh": 0, "total_power_export_t2_kwh": 0} ) ) 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) entry = entity_registry.async_get( "sensor.product_name_aabbccddeeff_total_power_export_t1" ) assert entry assert entry.disabled entry = entity_registry.async_get( "sensor.product_name_aabbccddeeff_total_power_export_t2" ) assert entry assert entry.disabled