From dd69ba31366e9baf0612fc537d326f6f26953101 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Thu, 17 Aug 2023 18:29:20 +0200 Subject: [PATCH] Migrate Cert Expiry to has entity name (#98160) * Migrate Cert Expiry to has entity name * Migrate Cert Expiry to has entity name * Fix entity name --- homeassistant/components/cert_expiry/sensor.py | 3 ++- .../components/cert_expiry/strings.json | 7 +++++++ tests/components/cert_expiry/test_init.py | 10 +++++----- tests/components/cert_expiry/test_sensors.py | 18 +++++++++--------- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/cert_expiry/sensor.py b/homeassistant/components/cert_expiry/sensor.py index aeae8a5afe9..645642067e6 100644 --- a/homeassistant/components/cert_expiry/sensor.py +++ b/homeassistant/components/cert_expiry/sensor.py @@ -77,6 +77,7 @@ class CertExpiryEntity(CoordinatorEntity[CertExpiryDataUpdateCoordinator]): """Defines a base Cert Expiry entity.""" _attr_icon = "mdi:certificate" + _attr_has_entity_name = True @property def extra_state_attributes(self): @@ -91,6 +92,7 @@ class SSLCertificateTimestamp(CertExpiryEntity, SensorEntity): """Implementation of the Cert Expiry timestamp sensor.""" _attr_device_class = SensorDeviceClass.TIMESTAMP + _attr_translation_key = "certificate_expiry" def __init__( self, @@ -98,7 +100,6 @@ class SSLCertificateTimestamp(CertExpiryEntity, SensorEntity): ) -> None: """Initialize a Cert Expiry timestamp sensor.""" super().__init__(coordinator) - self._attr_name = f"Cert Expiry Timestamp ({coordinator.name})" self._attr_unique_id = f"{coordinator.host}:{coordinator.port}-timestamp" self._attr_device_info = DeviceInfo( identifiers={(DOMAIN, f"{coordinator.host}:{coordinator.port}")}, diff --git a/homeassistant/components/cert_expiry/strings.json b/homeassistant/components/cert_expiry/strings.json index 5c8af4df931..b8c7ffe037f 100644 --- a/homeassistant/components/cert_expiry/strings.json +++ b/homeassistant/components/cert_expiry/strings.json @@ -20,5 +20,12 @@ "already_configured": "[%key:common::config_flow::abort::already_configured_service%]", "import_failed": "Import from config failed" } + }, + "entity": { + "sensor": { + "certificate_expiry": { + "name": "Cert expiry" + } + } } } diff --git a/tests/components/cert_expiry/test_init.py b/tests/components/cert_expiry/test_init.py index 2113ff5cc42..29fbf372ec4 100644 --- a/tests/components/cert_expiry/test_init.py +++ b/tests/components/cert_expiry/test_init.py @@ -99,7 +99,7 @@ async def test_unload_config_entry(mock_now, hass: HomeAssistant) -> None: await hass.async_block_till_done() assert entry.state is ConfigEntryState.LOADED - state = hass.states.get("sensor.cert_expiry_timestamp_example_com") + state = hass.states.get("sensor.example_com_cert_expiry") assert state.state == timestamp.isoformat() assert state.attributes.get("error") == "None" assert state.attributes.get("is_valid") @@ -107,12 +107,12 @@ async def test_unload_config_entry(mock_now, hass: HomeAssistant) -> None: await hass.config_entries.async_unload(entry.entry_id) assert entry.state is ConfigEntryState.NOT_LOADED - state = hass.states.get("sensor.cert_expiry_timestamp_example_com") + state = hass.states.get("sensor.example_com_cert_expiry") assert state.state == STATE_UNAVAILABLE await hass.config_entries.async_remove(entry.entry_id) await hass.async_block_till_done() - state = hass.states.get("sensor.cert_expiry_timestamp_example_com") + state = hass.states.get("sensor.example_com_cert_expiry") assert state is None @@ -129,7 +129,7 @@ async def test_delay_load_during_startup(hass: HomeAssistant) -> None: assert hass.state is CoreState.not_running assert entry.state is ConfigEntryState.LOADED - state = hass.states.get("sensor.cert_expiry_timestamp_example_com") + state = hass.states.get("sensor.example_com_cert_expiry") assert state is None timestamp = future_timestamp(100) @@ -142,7 +142,7 @@ async def test_delay_load_during_startup(hass: HomeAssistant) -> None: assert hass.state is CoreState.running - state = hass.states.get("sensor.cert_expiry_timestamp_example_com") + state = hass.states.get("sensor.example_com_cert_expiry") assert state.state == timestamp.isoformat() assert state.attributes.get("error") == "None" assert state.attributes.get("is_valid") diff --git a/tests/components/cert_expiry/test_sensors.py b/tests/components/cert_expiry/test_sensors.py index 0fbf276cdea..e6a526c7c9e 100644 --- a/tests/components/cert_expiry/test_sensors.py +++ b/tests/components/cert_expiry/test_sensors.py @@ -36,7 +36,7 @@ async def test_async_setup_entry(mock_now, hass: HomeAssistant) -> None: assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() - state = hass.states.get("sensor.cert_expiry_timestamp_example_com") + state = hass.states.get("sensor.example_com_cert_expiry") assert state is not None assert state.state != STATE_UNAVAILABLE assert state.state == timestamp.isoformat() @@ -62,7 +62,7 @@ async def test_async_setup_entry_bad_cert(hass: HomeAssistant) -> None: assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() - state = hass.states.get("sensor.cert_expiry_timestamp_example_com") + state = hass.states.get("sensor.example_com_cert_expiry") assert state is not None assert state.state != STATE_UNAVAILABLE assert state.attributes.get("error") == "some error" @@ -90,7 +90,7 @@ async def test_update_sensor(hass: HomeAssistant) -> None: assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() - state = hass.states.get("sensor.cert_expiry_timestamp_example_com") + state = hass.states.get("sensor.example_com_cert_expiry") assert state is not None assert state.state != STATE_UNAVAILABLE assert state.state == timestamp.isoformat() @@ -105,7 +105,7 @@ async def test_update_sensor(hass: HomeAssistant) -> None: async_fire_time_changed(hass, utcnow() + timedelta(hours=24)) await hass.async_block_till_done() - state = hass.states.get("sensor.cert_expiry_timestamp_example_com") + state = hass.states.get("sensor.example_com_cert_expiry") assert state is not None assert state.state != STATE_UNAVAILABLE assert state.state == timestamp.isoformat() @@ -134,7 +134,7 @@ async def test_update_sensor_network_errors(hass: HomeAssistant) -> None: assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() - state = hass.states.get("sensor.cert_expiry_timestamp_example_com") + state = hass.states.get("sensor.example_com_cert_expiry") assert state is not None assert state.state != STATE_UNAVAILABLE assert state.state == timestamp.isoformat() @@ -152,7 +152,7 @@ async def test_update_sensor_network_errors(hass: HomeAssistant) -> None: next_update = starting_time + timedelta(hours=48) - state = hass.states.get("sensor.cert_expiry_timestamp_example_com") + state = hass.states.get("sensor.example_com_cert_expiry") assert state.state == STATE_UNAVAILABLE with patch("homeassistant.util.dt.utcnow", return_value=next_update), patch( @@ -162,7 +162,7 @@ async def test_update_sensor_network_errors(hass: HomeAssistant) -> None: async_fire_time_changed(hass, utcnow() + timedelta(hours=48)) await hass.async_block_till_done() - state = hass.states.get("sensor.cert_expiry_timestamp_example_com") + state = hass.states.get("sensor.example_com_cert_expiry") assert state is not None assert state.state != STATE_UNAVAILABLE assert state.state == timestamp.isoformat() @@ -178,7 +178,7 @@ async def test_update_sensor_network_errors(hass: HomeAssistant) -> None: async_fire_time_changed(hass, utcnow() + timedelta(hours=72)) await hass.async_block_till_done() - state = hass.states.get("sensor.cert_expiry_timestamp_example_com") + state = hass.states.get("sensor.example_com_cert_expiry") assert state is not None assert state.state == STATE_UNKNOWN assert state.attributes.get("error") == "something bad" @@ -192,5 +192,5 @@ async def test_update_sensor_network_errors(hass: HomeAssistant) -> None: async_fire_time_changed(hass, utcnow() + timedelta(hours=96)) await hass.async_block_till_done() - state = hass.states.get("sensor.cert_expiry_timestamp_example_com") + state = hass.states.get("sensor.example_com_cert_expiry") assert state.state == STATE_UNAVAILABLE