From 9088a6a13816652aabf7337a924dfdbc387d745e Mon Sep 17 00:00:00 2001 From: avee87 <6134677+avee87@users.noreply.github.com> Date: Tue, 23 Nov 2021 21:45:23 +0000 Subject: [PATCH] Add device registry entry for MetOffice (#58683) --- .../components/metoffice/__init__.py | 11 ++++++++++ homeassistant/components/metoffice/sensor.py | 4 ++++ homeassistant/components/metoffice/weather.py | 20 +++++++------------ tests/components/metoffice/const.py | 8 ++++++++ tests/components/metoffice/test_sensor.py | 15 ++++++++++++++ tests/components/metoffice/test_weather.py | 18 +++++++++++++++++ 6 files changed, 63 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/metoffice/__init__.py b/homeassistant/components/metoffice/__init__.py index 4c187a606c7..2d762eb7c3b 100644 --- a/homeassistant/components/metoffice/__init__.py +++ b/homeassistant/components/metoffice/__init__.py @@ -9,6 +9,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady +from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from .const import ( @@ -98,3 +99,13 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: if not hass.data[DOMAIN]: hass.data.pop(DOMAIN) return unload_ok + + +def get_device_info(coordinates: str, name: str) -> DeviceInfo: + """Return device registry information.""" + return DeviceInfo( + entry_type="service", + identifiers={(DOMAIN, coordinates)}, + manufacturer="Met Office", + name=f"Met Office {name}", + ) diff --git a/homeassistant/components/metoffice/sensor.py b/homeassistant/components/metoffice/sensor.py index 4919e36bd58..4f2ed842086 100644 --- a/homeassistant/components/metoffice/sensor.py +++ b/homeassistant/components/metoffice/sensor.py @@ -16,6 +16,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.update_coordinator import CoordinatorEntity +from . import get_device_info from .const import ( ATTRIBUTION, CONDITION_CLASSES, @@ -181,6 +182,9 @@ class MetOfficeCurrentSensor(CoordinatorEntity, SensorEntity): self.entity_description = description mode_label = MODE_3HOURLY_LABEL if use_3hourly else MODE_DAILY_LABEL + self._attr_device_info = get_device_info( + coordinates=hass_data[METOFFICE_COORDINATES], name=hass_data[METOFFICE_NAME] + ) self._attr_name = f"{hass_data[METOFFICE_NAME]} {description.name} {mode_label}" self._attr_unique_id = f"{description.name}_{hass_data[METOFFICE_COORDINATES]}" if not use_3hourly: diff --git a/homeassistant/components/metoffice/weather.py b/homeassistant/components/metoffice/weather.py index b02539f0e31..56d94409db5 100644 --- a/homeassistant/components/metoffice/weather.py +++ b/homeassistant/components/metoffice/weather.py @@ -13,6 +13,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.update_coordinator import CoordinatorEntity +from . import get_device_info from .const import ( ATTRIBUTION, CONDITION_CLASSES, @@ -76,20 +77,13 @@ class MetOfficeWeather(CoordinatorEntity, WeatherEntity): super().__init__(coordinator) mode_label = MODE_3HOURLY_LABEL if use_3hourly else MODE_DAILY_LABEL - self._name = f"{DEFAULT_NAME} {hass_data[METOFFICE_NAME]} {mode_label}" - self._unique_id = hass_data[METOFFICE_COORDINATES] + self._attr_device_info = get_device_info( + coordinates=hass_data[METOFFICE_COORDINATES], name=hass_data[METOFFICE_NAME] + ) + self._attr_name = f"{DEFAULT_NAME} {hass_data[METOFFICE_NAME]} {mode_label}" + self._attr_unique_id = hass_data[METOFFICE_COORDINATES] if not use_3hourly: - self._unique_id = f"{self._unique_id}_{MODE_DAILY}" - - @property - def name(self): - """Return the name of the sensor.""" - return self._name - - @property - def unique_id(self): - """Return the unique of the sensor.""" - return self._unique_id + self._attr_unique_id = f"{self._attr_unique_id}_{MODE_DAILY}" @property def condition(self): diff --git a/tests/components/metoffice/const.py b/tests/components/metoffice/const.py index c9a173e3f12..56383764d08 100644 --- a/tests/components/metoffice/const.py +++ b/tests/components/metoffice/const.py @@ -1,5 +1,6 @@ """Helpers for testing Met Office DataPoint.""" +from homeassistant.components.metoffice.const import DOMAIN from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME TEST_DATETIME_STRING = "2020-04-25T12:00:00+00:00" @@ -55,3 +56,10 @@ WAVERTREE_SENSOR_RESULTS = { "wind_speed": ("wind_speed", "9"), "humidity": ("humidity", "50"), } + +DEVICE_KEY_KINGSLYNN = { + (DOMAIN, f"{TEST_LATITUDE_KINGSLYNN}_{TEST_LONGITUDE_KINGSLYNN}") +} +DEVICE_KEY_WAVERTREE = { + (DOMAIN, f"{TEST_LATITUDE_WAVERTREE}_{TEST_LONGITUDE_WAVERTREE}") +} diff --git a/tests/components/metoffice/test_sensor.py b/tests/components/metoffice/test_sensor.py index 201c5922d33..b8b2458dc5b 100644 --- a/tests/components/metoffice/test_sensor.py +++ b/tests/components/metoffice/test_sensor.py @@ -3,9 +3,12 @@ import json from unittest.mock import patch from homeassistant.components.metoffice.const import ATTRIBUTION, DOMAIN +from homeassistant.helpers.device_registry import async_get as get_dev_reg from . import NewDateTime from .const import ( + DEVICE_KEY_KINGSLYNN, + DEVICE_KEY_WAVERTREE, KINGSLYNN_SENSOR_RESULTS, METOFFICE_CONFIG_KINGSLYNN, METOFFICE_CONFIG_WAVERTREE, @@ -48,6 +51,11 @@ async def test_one_sensor_site_running(hass, requests_mock, legacy_patchable_tim await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() + dev_reg = get_dev_reg(hass) + assert len(dev_reg.devices) == 1 + device_wavertree = dev_reg.async_get_device(identifiers=DEVICE_KEY_WAVERTREE) + assert device_wavertree.name == "Met Office Wavertree" + running_sensor_ids = hass.states.async_entity_ids("sensor") assert len(running_sensor_ids) > 0 for running_id in running_sensor_ids: @@ -105,6 +113,13 @@ async def test_two_sensor_sites_running(hass, requests_mock, legacy_patchable_ti await hass.config_entries.async_setup(entry2.entry_id) await hass.async_block_till_done() + dev_reg = get_dev_reg(hass) + assert len(dev_reg.devices) == 2 + device_kingslynn = dev_reg.async_get_device(identifiers=DEVICE_KEY_KINGSLYNN) + assert device_kingslynn.name == "Met Office King's Lynn" + device_wavertree = dev_reg.async_get_device(identifiers=DEVICE_KEY_WAVERTREE) + assert device_wavertree.name == "Met Office Wavertree" + running_sensor_ids = hass.states.async_entity_ids("sensor") assert len(running_sensor_ids) > 0 for running_id in running_sensor_ids: diff --git a/tests/components/metoffice/test_weather.py b/tests/components/metoffice/test_weather.py index 76e01b638c3..4fdd80f8277 100644 --- a/tests/components/metoffice/test_weather.py +++ b/tests/components/metoffice/test_weather.py @@ -5,10 +5,13 @@ from unittest.mock import patch from homeassistant.components.metoffice.const import DOMAIN from homeassistant.const import STATE_UNAVAILABLE +from homeassistant.helpers.device_registry import async_get as get_dev_reg from homeassistant.util import utcnow from . import NewDateTime from .const import ( + DEVICE_KEY_KINGSLYNN, + DEVICE_KEY_WAVERTREE, METOFFICE_CONFIG_KINGSLYNN, METOFFICE_CONFIG_WAVERTREE, WAVERTREE_SENSOR_RESULTS, @@ -36,6 +39,9 @@ async def test_site_cannot_connect(hass, requests_mock, legacy_patchable_time): await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() + dev_reg = get_dev_reg(hass) + assert len(dev_reg.devices) == 0 + assert hass.states.get("weather.met_office_wavertree_3hourly") is None assert hass.states.get("weather.met_office_wavertree_daily") is None for sensor_id in WAVERTREE_SENSOR_RESULTS: @@ -124,6 +130,11 @@ async def test_one_weather_site_running(hass, requests_mock, legacy_patchable_ti await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() + dev_reg = get_dev_reg(hass) + assert len(dev_reg.devices) == 1 + device_wavertree = dev_reg.async_get_device(identifiers=DEVICE_KEY_WAVERTREE) + assert device_wavertree.name == "Met Office Wavertree" + # Wavertree 3-hourly weather platform expected results weather = hass.states.get("weather.met_office_wavertree_3_hourly") assert weather @@ -213,6 +224,13 @@ async def test_two_weather_sites_running(hass, requests_mock, legacy_patchable_t await hass.config_entries.async_setup(entry2.entry_id) await hass.async_block_till_done() + dev_reg = get_dev_reg(hass) + assert len(dev_reg.devices) == 2 + device_kingslynn = dev_reg.async_get_device(identifiers=DEVICE_KEY_KINGSLYNN) + assert device_kingslynn.name == "Met Office King's Lynn" + device_wavertree = dev_reg.async_get_device(identifiers=DEVICE_KEY_WAVERTREE) + assert device_wavertree.name == "Met Office Wavertree" + # Wavertree 3-hourly weather platform expected results weather = hass.states.get("weather.met_office_wavertree_3_hourly") assert weather