Fix station name sensor for metoffice (#145500)

This commit is contained in:
avee87 2025-07-01 15:18:13 +01:00 committed by GitHub
parent 59bf39f4ed
commit 655f009f07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 7 deletions

View File

@ -9,6 +9,7 @@ from datapoint.Forecast import Forecast
from homeassistant.components.sensor import (
DOMAIN as SENSOR_DOMAIN,
EntityCategory,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
@ -59,6 +60,7 @@ SENSOR_TYPES: tuple[MetOfficeSensorEntityDescription, ...] = (
native_attr_name="name",
name="Station name",
icon="mdi:label-outline",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
MetOfficeSensorEntityDescription(
@ -235,14 +237,13 @@ class MetOfficeCurrentSensor(
@property
def native_value(self) -> StateType:
"""Return the state of the sensor."""
value = get_attribute(
self.coordinator.data.now(), self.entity_description.native_attr_name
)
native_attr = self.entity_description.native_attr_name
if (
self.entity_description.native_attr_name == "significantWeatherCode"
and value is not None
):
if native_attr == "name":
return str(self.coordinator.data.name)
value = get_attribute(self.coordinator.data.now(), native_attr)
if native_attr == "significantWeatherCode" and value is not None:
value = CONDITION_MAP.get(value)
return value

View File

@ -40,6 +40,12 @@ KINGSLYNN_SENSOR_RESULTS = {
"probability_of_precipitation": "67",
"pressure": "998.20",
"wind_speed": "22.21",
"wind_direction": "180",
"wind_gust": "40.26",
"feels_like_temperature": "3.4",
"visibility_distance": "7478.00",
"humidity": "97.5",
"station_name": "King's Lynn",
}
WAVERTREE_SENSOR_RESULTS = {
@ -49,6 +55,12 @@ WAVERTREE_SENSOR_RESULTS = {
"probability_of_precipitation": "61",
"pressure": "987.50",
"wind_speed": "17.60",
"wind_direction": "176",
"wind_gust": "34.52",
"feels_like_temperature": "5.8",
"visibility_distance": "5106.00",
"humidity": "95.13",
"station_name": "Wavertree",
}
DEVICE_KEY_KINGSLYNN = {(DOMAIN, TEST_COORDINATES_KINGSLYNN)}

View File

@ -28,6 +28,7 @@ from tests.common import MockConfigEntry, async_load_fixture, get_sensor_display
@pytest.mark.freeze_time(datetime.datetime(2024, 11, 23, 12, tzinfo=datetime.UTC))
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_one_sensor_site_running(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
@ -78,6 +79,7 @@ async def test_one_sensor_site_running(
@pytest.mark.freeze_time(datetime.datetime(2024, 11, 23, 12, tzinfo=datetime.UTC))
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_two_sensor_sites_running(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,