Add device registry entry for MetOffice (#58683)

This commit is contained in:
avee87 2021-11-23 21:45:23 +00:00 committed by GitHub
parent 6089aef072
commit 9088a6a138
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 13 deletions

View File

@ -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}",
)

View File

@ -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:

View File

@ -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):

View File

@ -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}")
}

View File

@ -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:

View File

@ -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