From 1bb32a05a9f5478f6b856541ca7efebc4a8b98c5 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Sat, 26 Oct 2024 02:28:26 +0200 Subject: [PATCH] Migrate Smarty to has entity name (#129145) --- .../components/smarty/binary_sensor.py | 7 ++- homeassistant/components/smarty/entity.py | 12 +++++ homeassistant/components/smarty/fan.py | 4 +- homeassistant/components/smarty/icons.json | 9 ++++ homeassistant/components/smarty/sensor.py | 13 +++-- homeassistant/components/smarty/strings.json | 33 +++++++++++++ .../smarty/snapshots/test_binary_sensor.ambr | 20 ++++---- .../components/smarty/snapshots/test_fan.ambr | 9 ++-- .../smarty/snapshots/test_init.ambr | 33 +++++++++++++ .../smarty/snapshots/test_sensor.ambr | 48 +++++++++---------- tests/components/smarty/test_init.py | 22 ++++++++- 11 files changed, 157 insertions(+), 53 deletions(-) create mode 100644 homeassistant/components/smarty/icons.json create mode 100644 tests/components/smarty/snapshots/test_init.ambr diff --git a/homeassistant/components/smarty/binary_sensor.py b/homeassistant/components/smarty/binary_sensor.py index a0282d5b31d..213cb00d47c 100644 --- a/homeassistant/components/smarty/binary_sensor.py +++ b/homeassistant/components/smarty/binary_sensor.py @@ -32,19 +32,19 @@ class SmartyBinarySensorEntityDescription(BinarySensorEntityDescription): ENTITIES: tuple[SmartyBinarySensorEntityDescription, ...] = ( SmartyBinarySensorEntityDescription( key="alarm", - name="Alarm", + translation_key="alarm", device_class=BinarySensorDeviceClass.PROBLEM, value_fn=lambda smarty: smarty.alarm, ), SmartyBinarySensorEntityDescription( key="warning", - name="Warning", + translation_key="warning", device_class=BinarySensorDeviceClass.PROBLEM, value_fn=lambda smarty: smarty.warning, ), SmartyBinarySensorEntityDescription( key="boost", - name="Boost State", + translation_key="boost_state", value_fn=lambda smarty: smarty.boost, ), ) @@ -77,7 +77,6 @@ class SmartyBinarySensor(SmartyEntity, BinarySensorEntity): """Initialize the entity.""" super().__init__(coordinator) self.entity_description = entity_description - self._attr_name = f"{coordinator.config_entry.title} {entity_description.name}" self._attr_unique_id = ( f"{coordinator.config_entry.entry_id}_{entity_description.key}" ) diff --git a/homeassistant/components/smarty/entity.py b/homeassistant/components/smarty/entity.py index c9ac1139b87..92f73e2ace7 100644 --- a/homeassistant/components/smarty/entity.py +++ b/homeassistant/components/smarty/entity.py @@ -1,9 +1,21 @@ """Smarty Entity class.""" +from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity +from . import DOMAIN from .coordinator import SmartyCoordinator class SmartyEntity(CoordinatorEntity[SmartyCoordinator]): """Representation of a Smarty Entity.""" + + _attr_has_entity_name = True + + def __init__(self, coordinator: SmartyCoordinator) -> None: + """Initialize the entity.""" + super().__init__(coordinator) + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, coordinator.config_entry.entry_id)}, + manufacturer="Salda", + ) diff --git a/homeassistant/components/smarty/fan.py b/homeassistant/components/smarty/fan.py index e9d6b1df37a..378585a33e1 100644 --- a/homeassistant/components/smarty/fan.py +++ b/homeassistant/components/smarty/fan.py @@ -41,7 +41,8 @@ async def async_setup_entry( class SmartyFan(SmartyEntity, FanEntity): """Representation of a Smarty Fan.""" - _attr_icon = "mdi:air-conditioner" + _attr_name = None + _attr_translation_key = "fan" _attr_supported_features = ( FanEntityFeature.SET_SPEED | FanEntityFeature.TURN_OFF @@ -52,7 +53,6 @@ class SmartyFan(SmartyEntity, FanEntity): def __init__(self, coordinator: SmartyCoordinator) -> None: """Initialize the entity.""" super().__init__(coordinator) - self._attr_name = coordinator.config_entry.title self._smarty_fan_speed = 0 self._smarty = coordinator.client self._attr_unique_id = coordinator.config_entry.entry_id diff --git a/homeassistant/components/smarty/icons.json b/homeassistant/components/smarty/icons.json new file mode 100644 index 00000000000..97e74199f0a --- /dev/null +++ b/homeassistant/components/smarty/icons.json @@ -0,0 +1,9 @@ +{ + "entity": { + "fan": { + "fan": { + "default": "mdi:air-conditioner" + } + } + } +} diff --git a/homeassistant/components/smarty/sensor.py b/homeassistant/components/smarty/sensor.py index f720abfbbf6..90a2d1eade2 100644 --- a/homeassistant/components/smarty/sensor.py +++ b/homeassistant/components/smarty/sensor.py @@ -42,38 +42,38 @@ class SmartySensorDescription(SensorEntityDescription): ENTITIES: tuple[SmartySensorDescription, ...] = ( SmartySensorDescription( key="supply_air_temperature", - name="Supply Air Temperature", + translation_key="supply_air_temperature", device_class=SensorDeviceClass.TEMPERATURE, native_unit_of_measurement=UnitOfTemperature.CELSIUS, value_fn=lambda smarty: smarty.supply_air_temperature, ), SmartySensorDescription( key="extract_air_temperature", - name="Extract Air Temperature", + translation_key="extract_air_temperature", device_class=SensorDeviceClass.TEMPERATURE, native_unit_of_measurement=UnitOfTemperature.CELSIUS, value_fn=lambda smarty: smarty.extract_air_temperature, ), SmartySensorDescription( key="outdoor_air_temperature", - name="Outdoor Air Temperature", + translation_key="outdoor_air_temperature", device_class=SensorDeviceClass.TEMPERATURE, native_unit_of_measurement=UnitOfTemperature.CELSIUS, value_fn=lambda smarty: smarty.outdoor_air_temperature, ), SmartySensorDescription( key="supply_fan_speed", - name="Supply Fan Speed", + translation_key="supply_fan_speed", value_fn=lambda smarty: smarty.supply_fan_speed, ), SmartySensorDescription( key="extract_fan_speed", - name="Extract Fan Speed", + translation_key="extract_fan_speed", value_fn=lambda smarty: smarty.extract_fan_speed, ), SmartySensorDescription( key="filter_days_left", - name="Filter Days Left", + translation_key="filter_days_left", device_class=SensorDeviceClass.TIMESTAMP, value_fn=get_filter_days_left, ), @@ -107,7 +107,6 @@ class SmartySensor(SmartyEntity, SensorEntity): """Initialize the entity.""" super().__init__(coordinator) self.entity_description = entity_description - self._attr_name = f"{coordinator.config_entry.title} {entity_description.name}" self._attr_unique_id = ( f"{coordinator.config_entry.entry_id}_{entity_description.key}" ) diff --git a/homeassistant/components/smarty/strings.json b/homeassistant/components/smarty/strings.json index dedc717da30..367a3a34625 100644 --- a/homeassistant/components/smarty/strings.json +++ b/homeassistant/components/smarty/strings.json @@ -29,5 +29,38 @@ "title": "YAML import failed due to an authentication error", "description": "Configuring {integration_title} using YAML is being removed but there was an authentication error while importing your existing configuration.\nSetup will not proceed.\n\nVerify that your {integration_title} is operating correctly and restart Home Assistant to attempt the import again.\n\nAlternatively, you may remove the `{domain}` configuration from your configuration.yaml entirely, restart Home Assistant, and add the {integration_title} integration manually." } + }, + "entity": { + "binary_sensor": { + "alarm": { + "name": "Alarm" + }, + "warning": { + "name": "Warning" + }, + "boost_state": { + "name": "Boost state" + } + }, + "sensor": { + "supply_air_temperature": { + "name": "Supply air temperature" + }, + "extract_air_temperature": { + "name": "Extract air temperature" + }, + "outdoor_air_temperature": { + "name": "Outdoor air temperature" + }, + "supply_fan_speed": { + "name": "Supply fan speed" + }, + "extract_fan_speed": { + "name": "Extract fan speed" + }, + "filter_days_left": { + "name": "Filter days left" + } + } } } diff --git a/tests/components/smarty/snapshots/test_binary_sensor.ambr b/tests/components/smarty/snapshots/test_binary_sensor.ambr index 3d261e607a4..2f943a25012 100644 --- a/tests/components/smarty/snapshots/test_binary_sensor.ambr +++ b/tests/components/smarty/snapshots/test_binary_sensor.ambr @@ -12,7 +12,7 @@ 'domain': 'binary_sensor', 'entity_category': None, 'entity_id': 'binary_sensor.mock_title_alarm', - 'has_entity_name': False, + 'has_entity_name': True, 'hidden_by': None, 'icon': None, 'id': , @@ -23,11 +23,11 @@ }), 'original_device_class': , 'original_icon': None, - 'original_name': 'Mock Title Alarm', + 'original_name': 'Alarm', 'platform': 'smarty', 'previous_unique_id': None, 'supported_features': 0, - 'translation_key': None, + 'translation_key': 'alarm', 'unique_id': '01JAZ5DPW8C62D620DGYNG2R8H_alarm', 'unit_of_measurement': None, }) @@ -59,7 +59,7 @@ 'domain': 'binary_sensor', 'entity_category': None, 'entity_id': 'binary_sensor.mock_title_boost_state', - 'has_entity_name': False, + 'has_entity_name': True, 'hidden_by': None, 'icon': None, 'id': , @@ -70,11 +70,11 @@ }), 'original_device_class': None, 'original_icon': None, - 'original_name': 'Mock Title Boost State', + 'original_name': 'Boost state', 'platform': 'smarty', 'previous_unique_id': None, 'supported_features': 0, - 'translation_key': None, + 'translation_key': 'boost_state', 'unique_id': '01JAZ5DPW8C62D620DGYNG2R8H_boost', 'unit_of_measurement': None, }) @@ -82,7 +82,7 @@ # name: test_all_entities[binary_sensor.mock_title_boost_state-state] StateSnapshot({ 'attributes': ReadOnlyDict({ - 'friendly_name': 'Mock Title Boost State', + 'friendly_name': 'Mock Title Boost state', }), 'context': , 'entity_id': 'binary_sensor.mock_title_boost_state', @@ -105,7 +105,7 @@ 'domain': 'binary_sensor', 'entity_category': None, 'entity_id': 'binary_sensor.mock_title_warning', - 'has_entity_name': False, + 'has_entity_name': True, 'hidden_by': None, 'icon': None, 'id': , @@ -116,11 +116,11 @@ }), 'original_device_class': , 'original_icon': None, - 'original_name': 'Mock Title Warning', + 'original_name': 'Warning', 'platform': 'smarty', 'previous_unique_id': None, 'supported_features': 0, - 'translation_key': None, + 'translation_key': 'warning', 'unique_id': '01JAZ5DPW8C62D620DGYNG2R8H_warning', 'unit_of_measurement': None, }) diff --git a/tests/components/smarty/snapshots/test_fan.ambr b/tests/components/smarty/snapshots/test_fan.ambr index fe8743b1970..8ca95beeb86 100644 --- a/tests/components/smarty/snapshots/test_fan.ambr +++ b/tests/components/smarty/snapshots/test_fan.ambr @@ -14,7 +14,7 @@ 'domain': 'fan', 'entity_category': None, 'entity_id': 'fan.mock_title', - 'has_entity_name': False, + 'has_entity_name': True, 'hidden_by': None, 'icon': None, 'id': , @@ -24,12 +24,12 @@ 'options': dict({ }), 'original_device_class': None, - 'original_icon': 'mdi:air-conditioner', - 'original_name': 'Mock Title', + 'original_icon': None, + 'original_name': None, 'platform': 'smarty', 'previous_unique_id': None, 'supported_features': , - 'translation_key': None, + 'translation_key': 'fan', 'unique_id': '01JAZ5DPW8C62D620DGYNG2R8H', 'unit_of_measurement': None, }) @@ -38,7 +38,6 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'Mock Title', - 'icon': 'mdi:air-conditioner', 'percentage': 0, 'percentage_step': 33.333333333333336, 'preset_mode': None, diff --git a/tests/components/smarty/snapshots/test_init.ambr b/tests/components/smarty/snapshots/test_init.ambr new file mode 100644 index 00000000000..1545491c7d3 --- /dev/null +++ b/tests/components/smarty/snapshots/test_init.ambr @@ -0,0 +1,33 @@ +# serializer version: 1 +# name: test_device + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entries': , + 'configuration_url': None, + 'connections': set({ + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'smarty', + '01JAZ5DPW8C62D620DGYNG2R8H', + ), + }), + 'is_new': False, + 'labels': set({ + }), + 'manufacturer': 'Salda', + 'model': None, + 'model_id': None, + 'name': 'Mock Title', + 'name_by_user': None, + 'primary_config_entry': , + 'serial_number': None, + 'suggested_area': None, + 'sw_version': None, + 'via_device_id': None, + }) +# --- diff --git a/tests/components/smarty/snapshots/test_sensor.ambr b/tests/components/smarty/snapshots/test_sensor.ambr index 1fb8d79571c..2a5a6a33a84 100644 --- a/tests/components/smarty/snapshots/test_sensor.ambr +++ b/tests/components/smarty/snapshots/test_sensor.ambr @@ -12,7 +12,7 @@ 'domain': 'sensor', 'entity_category': None, 'entity_id': 'sensor.mock_title_extract_air_temperature', - 'has_entity_name': False, + 'has_entity_name': True, 'hidden_by': None, 'icon': None, 'id': , @@ -23,11 +23,11 @@ }), 'original_device_class': , 'original_icon': None, - 'original_name': 'Mock Title Extract Air Temperature', + 'original_name': 'Extract air temperature', 'platform': 'smarty', 'previous_unique_id': None, 'supported_features': 0, - 'translation_key': None, + 'translation_key': 'extract_air_temperature', 'unique_id': '01JAZ5DPW8C62D620DGYNG2R8H_extract_air_temperature', 'unit_of_measurement': , }) @@ -36,7 +36,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'temperature', - 'friendly_name': 'Mock Title Extract Air Temperature', + 'friendly_name': 'Mock Title Extract air temperature', 'unit_of_measurement': , }), 'context': , @@ -60,7 +60,7 @@ 'domain': 'sensor', 'entity_category': None, 'entity_id': 'sensor.mock_title_extract_fan_speed', - 'has_entity_name': False, + 'has_entity_name': True, 'hidden_by': None, 'icon': None, 'id': , @@ -71,11 +71,11 @@ }), 'original_device_class': None, 'original_icon': None, - 'original_name': 'Mock Title Extract Fan Speed', + 'original_name': 'Extract fan speed', 'platform': 'smarty', 'previous_unique_id': None, 'supported_features': 0, - 'translation_key': None, + 'translation_key': 'extract_fan_speed', 'unique_id': '01JAZ5DPW8C62D620DGYNG2R8H_extract_fan_speed', 'unit_of_measurement': None, }) @@ -83,7 +83,7 @@ # name: test_all_entities[sensor.mock_title_extract_fan_speed-state] StateSnapshot({ 'attributes': ReadOnlyDict({ - 'friendly_name': 'Mock Title Extract Fan Speed', + 'friendly_name': 'Mock Title Extract fan speed', }), 'context': , 'entity_id': 'sensor.mock_title_extract_fan_speed', @@ -106,7 +106,7 @@ 'domain': 'sensor', 'entity_category': None, 'entity_id': 'sensor.mock_title_filter_days_left', - 'has_entity_name': False, + 'has_entity_name': True, 'hidden_by': None, 'icon': None, 'id': , @@ -117,11 +117,11 @@ }), 'original_device_class': , 'original_icon': None, - 'original_name': 'Mock Title Filter Days Left', + 'original_name': 'Filter days left', 'platform': 'smarty', 'previous_unique_id': None, 'supported_features': 0, - 'translation_key': None, + 'translation_key': 'filter_days_left', 'unique_id': '01JAZ5DPW8C62D620DGYNG2R8H_filter_days_left', 'unit_of_measurement': None, }) @@ -130,7 +130,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'timestamp', - 'friendly_name': 'Mock Title Filter Days Left', + 'friendly_name': 'Mock Title Filter days left', }), 'context': , 'entity_id': 'sensor.mock_title_filter_days_left', @@ -153,7 +153,7 @@ 'domain': 'sensor', 'entity_category': None, 'entity_id': 'sensor.mock_title_outdoor_air_temperature', - 'has_entity_name': False, + 'has_entity_name': True, 'hidden_by': None, 'icon': None, 'id': , @@ -164,11 +164,11 @@ }), 'original_device_class': , 'original_icon': None, - 'original_name': 'Mock Title Outdoor Air Temperature', + 'original_name': 'Outdoor air temperature', 'platform': 'smarty', 'previous_unique_id': None, 'supported_features': 0, - 'translation_key': None, + 'translation_key': 'outdoor_air_temperature', 'unique_id': '01JAZ5DPW8C62D620DGYNG2R8H_outdoor_air_temperature', 'unit_of_measurement': , }) @@ -177,7 +177,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'temperature', - 'friendly_name': 'Mock Title Outdoor Air Temperature', + 'friendly_name': 'Mock Title Outdoor air temperature', 'unit_of_measurement': , }), 'context': , @@ -201,7 +201,7 @@ 'domain': 'sensor', 'entity_category': None, 'entity_id': 'sensor.mock_title_supply_air_temperature', - 'has_entity_name': False, + 'has_entity_name': True, 'hidden_by': None, 'icon': None, 'id': , @@ -212,11 +212,11 @@ }), 'original_device_class': , 'original_icon': None, - 'original_name': 'Mock Title Supply Air Temperature', + 'original_name': 'Supply air temperature', 'platform': 'smarty', 'previous_unique_id': None, 'supported_features': 0, - 'translation_key': None, + 'translation_key': 'supply_air_temperature', 'unique_id': '01JAZ5DPW8C62D620DGYNG2R8H_supply_air_temperature', 'unit_of_measurement': , }) @@ -225,7 +225,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'temperature', - 'friendly_name': 'Mock Title Supply Air Temperature', + 'friendly_name': 'Mock Title Supply air temperature', 'unit_of_measurement': , }), 'context': , @@ -249,7 +249,7 @@ 'domain': 'sensor', 'entity_category': None, 'entity_id': 'sensor.mock_title_supply_fan_speed', - 'has_entity_name': False, + 'has_entity_name': True, 'hidden_by': None, 'icon': None, 'id': , @@ -260,11 +260,11 @@ }), 'original_device_class': None, 'original_icon': None, - 'original_name': 'Mock Title Supply Fan Speed', + 'original_name': 'Supply fan speed', 'platform': 'smarty', 'previous_unique_id': None, 'supported_features': 0, - 'translation_key': None, + 'translation_key': 'supply_fan_speed', 'unique_id': '01JAZ5DPW8C62D620DGYNG2R8H_supply_fan_speed', 'unit_of_measurement': None, }) @@ -272,7 +272,7 @@ # name: test_all_entities[sensor.mock_title_supply_fan_speed-state] StateSnapshot({ 'attributes': ReadOnlyDict({ - 'friendly_name': 'Mock Title Supply Fan Speed', + 'friendly_name': 'Mock Title Supply fan speed', }), 'context': , 'entity_id': 'sensor.mock_title_supply_fan_speed', diff --git a/tests/components/smarty/test_init.py b/tests/components/smarty/test_init.py index 8c9100cb8b6..0366ea9eade 100644 --- a/tests/components/smarty/test_init.py +++ b/tests/components/smarty/test_init.py @@ -2,12 +2,16 @@ from unittest.mock import AsyncMock +from syrupy import SnapshotAssertion + from homeassistant.components.smarty import DOMAIN from homeassistant.const import CONF_HOST, CONF_NAME from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant -from homeassistant.helpers import issue_registry as ir +from homeassistant.helpers import device_registry as dr, issue_registry as ir from homeassistant.setup import async_setup_component +from . import setup_integration + from tests.common import MockConfigEntry @@ -60,3 +64,19 @@ async def test_import_flow_error( DOMAIN, "deprecated_yaml_import_issue_cannot_connect", ) in issue_registry.issues + + +async def test_device( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + mock_smarty: AsyncMock, + mock_config_entry: MockConfigEntry, + device_registry: dr.DeviceRegistry, +) -> None: + """Test device.""" + await setup_integration(hass, mock_config_entry) + device = device_registry.async_get_device( + identifiers={(DOMAIN, mock_config_entry.entry_id)} + ) + assert device + assert device == snapshot