diff --git a/tests/components/pvoutput/snapshots/test_sensor.ambr b/tests/components/pvoutput/snapshots/test_sensor.ambr new file mode 100644 index 000000000000..46cfb85fdb76 --- /dev/null +++ b/tests/components/pvoutput/snapshots/test_sensor.ambr @@ -0,0 +1,433 @@ +# serializer version: 1 +# name: test_device + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entry_id': , + 'config_subentry_id': , + 'configuration_url': 'https://pvoutput.org/list.jsp?sid=12345', + 'connections': set({ + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'pvoutput', + '12345', + ), + }), + 'labels': set({ + }), + 'manufacturer': 'PVOutput', + 'model': 'Super Inverters Inc.', + 'model_id': None, + 'name': "Frenck's Solar Farm", + 'name_by_user': None, + 'serial_number': None, + 'sw_version': None, + 'via_device_id': None, + }) +# --- +# name: test_sensors[sensor.frenck_s_solar_farm_efficiency-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.frenck_s_solar_farm_efficiency', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Efficiency', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Efficiency', + 'platform': 'pvoutput', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'efficiency', + 'unique_id': '12345_normalized_output', + 'unit_of_measurement': 'kWh/kW', + }) +# --- +# name: test_sensors[sensor.frenck_s_solar_farm_efficiency-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : "Frenck's Solar Farm Efficiency", + : , + : 'kWh/kW', + }), + 'context': , + 'entity_id': 'sensor.frenck_s_solar_farm_efficiency', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.5', + }) +# --- +# name: test_sensors[sensor.frenck_s_solar_farm_energy_consumption-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.frenck_s_solar_farm_energy_consumption', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Energy consumption', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Energy consumption', + 'platform': 'pvoutput', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'energy_consumption', + 'unique_id': '12345_energy_consumption', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.frenck_s_solar_farm_energy_consumption-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'energy', + : "Frenck's Solar Farm Energy consumption", + : , + : , + }), + 'context': , + 'entity_id': 'sensor.frenck_s_solar_farm_energy_consumption', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1000.0', + }) +# --- +# name: test_sensors[sensor.frenck_s_solar_farm_energy_generation-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.frenck_s_solar_farm_energy_generation', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Energy generation', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Energy generation', + 'platform': 'pvoutput', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'energy_generation', + 'unique_id': '12345_energy_generation', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.frenck_s_solar_farm_energy_generation-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'energy', + : "Frenck's Solar Farm Energy generation", + : , + : , + }), + 'context': , + 'entity_id': 'sensor.frenck_s_solar_farm_energy_generation', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '500.0', + }) +# --- +# name: test_sensors[sensor.frenck_s_solar_farm_power_consumption-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.frenck_s_solar_farm_power_consumption', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Power consumption', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Power consumption', + 'platform': 'pvoutput', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'power_consumption', + 'unique_id': '12345_power_consumption', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.frenck_s_solar_farm_power_consumption-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'power', + : "Frenck's Solar Farm Power consumption", + : , + : , + }), + 'context': , + 'entity_id': 'sensor.frenck_s_solar_farm_power_consumption', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2500.0', + }) +# --- +# name: test_sensors[sensor.frenck_s_solar_farm_power_generation-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.frenck_s_solar_farm_power_generation', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Power generation', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Power generation', + 'platform': 'pvoutput', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'power_generation', + 'unique_id': '12345_power_generation', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.frenck_s_solar_farm_power_generation-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'power', + : "Frenck's Solar Farm Power generation", + : , + : , + }), + 'context': , + 'entity_id': 'sensor.frenck_s_solar_farm_power_generation', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1500.0', + }) +# --- +# name: test_sensors[sensor.frenck_s_solar_farm_temperature-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.frenck_s_solar_farm_temperature', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Temperature', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Temperature', + 'platform': 'pvoutput', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '12345_temperature', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.frenck_s_solar_farm_temperature-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'temperature', + : "Frenck's Solar Farm Temperature", + : , + : , + }), + 'context': , + 'entity_id': 'sensor.frenck_s_solar_farm_temperature', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '20.2', + }) +# --- +# name: test_sensors[sensor.frenck_s_solar_farm_voltage-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.frenck_s_solar_farm_voltage', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Voltage', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Voltage', + 'platform': 'pvoutput', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '12345_voltage', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.frenck_s_solar_farm_voltage-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'voltage', + : "Frenck's Solar Farm Voltage", + : , + : , + }), + 'context': , + 'entity_id': 'sensor.frenck_s_solar_farm_voltage', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '220.5', + }) +# --- diff --git a/tests/components/pvoutput/test_sensor.py b/tests/components/pvoutput/test_sensor.py index 36a37653efee..f70b0666d9b4 100644 --- a/tests/components/pvoutput/test_sensor.py +++ b/tests/components/pvoutput/test_sensor.py @@ -1,151 +1,44 @@ """Tests for the sensors provided by the PVOutput integration.""" +import pytest +from syrupy.assertion import SnapshotAssertion + from homeassistant.components.pvoutput.const import DOMAIN -from homeassistant.components.sensor import ( - ATTR_STATE_CLASS, - SensorDeviceClass, - SensorStateClass, -) -from homeassistant.const import ( - ATTR_DEVICE_CLASS, - ATTR_FRIENDLY_NAME, - ATTR_ICON, - ATTR_UNIT_OF_MEASUREMENT, - UnitOfElectricPotential, - UnitOfEnergy, - UnitOfPower, - UnitOfTemperature, -) from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, snapshot_platform +@pytest.mark.usefixtures("init_integration") async def test_sensors( hass: HomeAssistant, - device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, - init_integration: MockConfigEntry, + snapshot: SnapshotAssertion, + mock_config_entry: MockConfigEntry, ) -> None: """Test the PVOutput sensors.""" + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) - state = hass.states.get("sensor.frenck_s_solar_farm_energy_consumption") - entry = entity_registry.async_get("sensor.frenck_s_solar_farm_energy_consumption") - assert entry - assert state - assert entry.unique_id == "12345_energy_consumption" - assert entry.entity_category is None - assert state.state == "1000.0" - assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY - assert ( - state.attributes.get(ATTR_FRIENDLY_NAME) - == "Frenck's Solar Farm Energy consumption" + +@pytest.mark.usefixtures("init_integration") +async def test_device( + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, + mock_config_entry: MockConfigEntry, +) -> None: + """Test the PVOutput device.""" + device_entry = device_registry.async_get_device_by_identifier( + (DOMAIN, "12345"), mock_config_entry.entry_id ) - assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.TOTAL_INCREASING - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.WATT_HOUR - assert ATTR_ICON not in state.attributes + assert device_entry is not None + assert device_entry == snapshot - state = hass.states.get("sensor.frenck_s_solar_farm_energy_generation") - entry = entity_registry.async_get("sensor.frenck_s_solar_farm_energy_generation") - assert entry - assert state - assert entry.unique_id == "12345_energy_generation" - assert entry.entity_category is None - assert state.state == "500.0" - assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY - assert ( - state.attributes.get(ATTR_FRIENDLY_NAME) - == "Frenck's Solar Farm Energy generation" + # The entity snapshots mask the device ID, so check the link explicitly + entity_entries = er.async_entries_for_config_entry( + entity_registry, mock_config_entry.entry_id ) - assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.TOTAL_INCREASING - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.WATT_HOUR - assert ATTR_ICON not in state.attributes - - state = hass.states.get("sensor.frenck_s_solar_farm_efficiency") - entry = entity_registry.async_get("sensor.frenck_s_solar_farm_efficiency") - assert entry - assert state - assert entry.unique_id == "12345_normalized_output" - assert entry.entity_category is None - assert state.state == "0.5" - assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Frenck's Solar Farm Efficiency" - assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT - assert ( - state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) - == f"{UnitOfEnergy.KILO_WATT_HOUR}/{UnitOfPower.KILO_WATT}" - ) - assert ATTR_DEVICE_CLASS not in state.attributes - assert ATTR_ICON not in state.attributes - - state = hass.states.get("sensor.frenck_s_solar_farm_power_consumption") - entry = entity_registry.async_get("sensor.frenck_s_solar_farm_power_consumption") - assert entry - assert state - assert entry.unique_id == "12345_power_consumption" - assert entry.entity_category is None - assert state.state == "2500.0" - assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER - assert ( - state.attributes.get(ATTR_FRIENDLY_NAME) - == "Frenck's Solar Farm Power consumption" - ) - assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT - assert ATTR_ICON not in state.attributes - - state = hass.states.get("sensor.frenck_s_solar_farm_power_generation") - entry = entity_registry.async_get("sensor.frenck_s_solar_farm_power_generation") - assert entry - assert state - assert entry.unique_id == "12345_power_generation" - assert entry.entity_category is None - assert state.state == "1500.0" - assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER - assert ( - state.attributes.get(ATTR_FRIENDLY_NAME) - == "Frenck's Solar Farm Power generation" - ) - assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT - assert ATTR_ICON not in state.attributes - - state = hass.states.get("sensor.frenck_s_solar_farm_temperature") - entry = entity_registry.async_get("sensor.frenck_s_solar_farm_temperature") - assert entry - assert state - assert entry.unique_id == "12345_temperature" - assert entry.entity_category is None - assert state.state == "20.2" - assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TEMPERATURE - assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Frenck's Solar Farm Temperature" - assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTemperature.CELSIUS - assert ATTR_ICON not in state.attributes - - state = hass.states.get("sensor.frenck_s_solar_farm_voltage") - entry = entity_registry.async_get("sensor.frenck_s_solar_farm_voltage") - assert entry - assert state - assert entry.unique_id == "12345_voltage" - assert entry.entity_category is None - assert state.state == "220.5" - assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.VOLTAGE - assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Frenck's Solar Farm Voltage" - assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT - assert ( - state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfElectricPotential.VOLT - ) - assert ATTR_ICON not in state.attributes - - assert entry.device_id - device_entry = device_registry.async_get(entry.device_id) - assert device_entry - assert device_entry.identifiers == {(DOMAIN, "12345")} - assert device_entry.manufacturer == "PVOutput" - assert device_entry.model == "Super Inverters Inc." - assert device_entry.name == "Frenck's Solar Farm" - assert device_entry.configuration_url == "https://pvoutput.org/list.jsp?sid=12345" - assert device_entry.entry_type is None - assert device_entry.sw_version is None - assert device_entry.hw_version is None + assert {entity_entry.device_id for entity_entry in entity_entries} == { + device_entry.id + }