mirror of
https://github.com/home-assistant/core.git
synced 2025-04-27 10:47:51 +00:00
New api endpoint for Trafikverket Weather (#104165)
* New api endpoint for Trafikverket Weather * fix tests
This commit is contained in:
parent
0eb8daee23
commit
47cd368ed2
@ -6,5 +6,5 @@
|
|||||||
"documentation": "https://www.home-assistant.io/integrations/trafikverket_camera",
|
"documentation": "https://www.home-assistant.io/integrations/trafikverket_camera",
|
||||||
"iot_class": "cloud_polling",
|
"iot_class": "cloud_polling",
|
||||||
"loggers": ["pytrafikverket"],
|
"loggers": ["pytrafikverket"],
|
||||||
"requirements": ["pytrafikverket==0.3.8"]
|
"requirements": ["pytrafikverket==0.3.9.1"]
|
||||||
}
|
}
|
||||||
|
@ -6,5 +6,5 @@
|
|||||||
"documentation": "https://www.home-assistant.io/integrations/trafikverket_ferry",
|
"documentation": "https://www.home-assistant.io/integrations/trafikverket_ferry",
|
||||||
"iot_class": "cloud_polling",
|
"iot_class": "cloud_polling",
|
||||||
"loggers": ["pytrafikverket"],
|
"loggers": ["pytrafikverket"],
|
||||||
"requirements": ["pytrafikverket==0.3.8"]
|
"requirements": ["pytrafikverket==0.3.9.1"]
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ from typing import Any
|
|||||||
from pytrafikverket import TrafikverketTrain
|
from pytrafikverket import TrafikverketTrain
|
||||||
from pytrafikverket.exceptions import (
|
from pytrafikverket.exceptions import (
|
||||||
InvalidAuthentication,
|
InvalidAuthentication,
|
||||||
MultipleTrainAnnouncementFound,
|
|
||||||
MultipleTrainStationsFound,
|
MultipleTrainStationsFound,
|
||||||
NoTrainAnnouncementFound,
|
NoTrainAnnouncementFound,
|
||||||
NoTrainStationFound,
|
NoTrainStationFound,
|
||||||
@ -107,8 +106,6 @@ async def validate_input(
|
|||||||
errors["base"] = "more_stations"
|
errors["base"] = "more_stations"
|
||||||
except NoTrainAnnouncementFound:
|
except NoTrainAnnouncementFound:
|
||||||
errors["base"] = "no_trains"
|
errors["base"] = "no_trains"
|
||||||
except MultipleTrainAnnouncementFound:
|
|
||||||
errors["base"] = "multiple_trains"
|
|
||||||
except UnknownError as error:
|
except UnknownError as error:
|
||||||
_LOGGER.error("Unknown error occurred during validation %s", str(error))
|
_LOGGER.error("Unknown error occurred during validation %s", str(error))
|
||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
|
@ -8,7 +8,6 @@ import logging
|
|||||||
from pytrafikverket import TrafikverketTrain
|
from pytrafikverket import TrafikverketTrain
|
||||||
from pytrafikverket.exceptions import (
|
from pytrafikverket.exceptions import (
|
||||||
InvalidAuthentication,
|
InvalidAuthentication,
|
||||||
MultipleTrainAnnouncementFound,
|
|
||||||
NoTrainAnnouncementFound,
|
NoTrainAnnouncementFound,
|
||||||
UnknownError,
|
UnknownError,
|
||||||
)
|
)
|
||||||
@ -112,7 +111,6 @@ class TVDataUpdateCoordinator(DataUpdateCoordinator[TrainData]):
|
|||||||
raise ConfigEntryAuthFailed from error
|
raise ConfigEntryAuthFailed from error
|
||||||
except (
|
except (
|
||||||
NoTrainAnnouncementFound,
|
NoTrainAnnouncementFound,
|
||||||
MultipleTrainAnnouncementFound,
|
|
||||||
UnknownError,
|
UnknownError,
|
||||||
) as error:
|
) as error:
|
||||||
raise UpdateFailed(
|
raise UpdateFailed(
|
||||||
|
@ -6,5 +6,5 @@
|
|||||||
"documentation": "https://www.home-assistant.io/integrations/trafikverket_train",
|
"documentation": "https://www.home-assistant.io/integrations/trafikverket_train",
|
||||||
"iot_class": "cloud_polling",
|
"iot_class": "cloud_polling",
|
||||||
"loggers": ["pytrafikverket"],
|
"loggers": ["pytrafikverket"],
|
||||||
"requirements": ["pytrafikverket==0.3.8"]
|
"requirements": ["pytrafikverket==0.3.9.1"]
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,6 @@
|
|||||||
"invalid_station": "Could not find a station with the specified name",
|
"invalid_station": "Could not find a station with the specified name",
|
||||||
"more_stations": "Found multiple stations with the specified name",
|
"more_stations": "Found multiple stations with the specified name",
|
||||||
"no_trains": "No train found",
|
"no_trains": "No train found",
|
||||||
"multiple_trains": "Multiple trains found",
|
|
||||||
"incorrect_api_key": "Invalid API key for selected account"
|
"incorrect_api_key": "Invalid API key for selected account"
|
||||||
},
|
},
|
||||||
"step": {
|
"step": {
|
||||||
|
@ -6,5 +6,5 @@
|
|||||||
"documentation": "https://www.home-assistant.io/integrations/trafikverket_weatherstation",
|
"documentation": "https://www.home-assistant.io/integrations/trafikverket_weatherstation",
|
||||||
"iot_class": "cloud_polling",
|
"iot_class": "cloud_polling",
|
||||||
"loggers": ["pytrafikverket"],
|
"loggers": ["pytrafikverket"],
|
||||||
"requirements": ["pytrafikverket==0.3.8"]
|
"requirements": ["pytrafikverket==0.3.9.1"]
|
||||||
}
|
}
|
||||||
|
@ -24,48 +24,17 @@ from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
from homeassistant.util.dt import as_utc
|
|
||||||
|
|
||||||
from .const import ATTRIBUTION, CONF_STATION, DOMAIN, NONE_IS_ZERO_SENSORS
|
from .const import ATTRIBUTION, CONF_STATION, DOMAIN, NONE_IS_ZERO_SENSORS
|
||||||
from .coordinator import TVDataUpdateCoordinator
|
from .coordinator import TVDataUpdateCoordinator
|
||||||
|
|
||||||
WIND_DIRECTIONS = [
|
|
||||||
"east",
|
|
||||||
"north_east",
|
|
||||||
"east_south_east",
|
|
||||||
"north",
|
|
||||||
"north_north_east",
|
|
||||||
"north_north_west",
|
|
||||||
"north_west",
|
|
||||||
"south",
|
|
||||||
"south_east",
|
|
||||||
"south_south_west",
|
|
||||||
"south_west",
|
|
||||||
"west",
|
|
||||||
]
|
|
||||||
PRECIPITATION_AMOUNTNAME = [
|
|
||||||
"error",
|
|
||||||
"mild_rain",
|
|
||||||
"moderate_rain",
|
|
||||||
"heavy_rain",
|
|
||||||
"mild_snow_rain",
|
|
||||||
"moderate_snow_rain",
|
|
||||||
"heavy_snow_rain",
|
|
||||||
"mild_snow",
|
|
||||||
"moderate_snow",
|
|
||||||
"heavy_snow",
|
|
||||||
"other",
|
|
||||||
"none",
|
|
||||||
"error",
|
|
||||||
]
|
|
||||||
PRECIPITATION_TYPE = [
|
PRECIPITATION_TYPE = [
|
||||||
"drizzle",
|
"no",
|
||||||
"hail",
|
|
||||||
"none",
|
|
||||||
"rain",
|
"rain",
|
||||||
"snow",
|
|
||||||
"rain_snow_mixed",
|
|
||||||
"freezing_rain",
|
"freezing_rain",
|
||||||
|
"snow",
|
||||||
|
"sleet",
|
||||||
|
"yes",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@ -103,8 +72,7 @@ SENSOR_TYPES: tuple[TrafikverketSensorEntityDescription, ...] = (
|
|||||||
TrafikverketSensorEntityDescription(
|
TrafikverketSensorEntityDescription(
|
||||||
key="precipitation",
|
key="precipitation",
|
||||||
translation_key="precipitation",
|
translation_key="precipitation",
|
||||||
api_key="precipitationtype_translated",
|
api_key="precipitationtype",
|
||||||
name="Precipitation type",
|
|
||||||
icon="mdi:weather-snowy-rainy",
|
icon="mdi:weather-snowy-rainy",
|
||||||
entity_registry_enabled_default=False,
|
entity_registry_enabled_default=False,
|
||||||
options=PRECIPITATION_TYPE,
|
options=PRECIPITATION_TYPE,
|
||||||
@ -114,20 +82,10 @@ SENSOR_TYPES: tuple[TrafikverketSensorEntityDescription, ...] = (
|
|||||||
key="wind_direction",
|
key="wind_direction",
|
||||||
translation_key="wind_direction",
|
translation_key="wind_direction",
|
||||||
api_key="winddirection",
|
api_key="winddirection",
|
||||||
name="Wind direction",
|
|
||||||
native_unit_of_measurement=DEGREE,
|
native_unit_of_measurement=DEGREE,
|
||||||
icon="mdi:flag-triangle",
|
icon="mdi:flag-triangle",
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
),
|
),
|
||||||
TrafikverketSensorEntityDescription(
|
|
||||||
key="wind_direction_text",
|
|
||||||
translation_key="wind_direction_text",
|
|
||||||
api_key="winddirectiontext_translated",
|
|
||||||
name="Wind direction text",
|
|
||||||
icon="mdi:flag-triangle",
|
|
||||||
options=WIND_DIRECTIONS,
|
|
||||||
device_class=SensorDeviceClass.ENUM,
|
|
||||||
),
|
|
||||||
TrafikverketSensorEntityDescription(
|
TrafikverketSensorEntityDescription(
|
||||||
key="wind_speed",
|
key="wind_speed",
|
||||||
api_key="windforce",
|
api_key="windforce",
|
||||||
@ -160,15 +118,6 @@ SENSOR_TYPES: tuple[TrafikverketSensorEntityDescription, ...] = (
|
|||||||
device_class=SensorDeviceClass.PRECIPITATION_INTENSITY,
|
device_class=SensorDeviceClass.PRECIPITATION_INTENSITY,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
),
|
),
|
||||||
TrafikverketSensorEntityDescription(
|
|
||||||
key="precipitation_amountname",
|
|
||||||
translation_key="precipitation_amountname",
|
|
||||||
api_key="precipitation_amountname_translated",
|
|
||||||
icon="mdi:weather-pouring",
|
|
||||||
entity_registry_enabled_default=False,
|
|
||||||
options=PRECIPITATION_AMOUNTNAME,
|
|
||||||
device_class=SensorDeviceClass.ENUM,
|
|
||||||
),
|
|
||||||
TrafikverketSensorEntityDescription(
|
TrafikverketSensorEntityDescription(
|
||||||
key="measure_time",
|
key="measure_time",
|
||||||
translation_key="measure_time",
|
translation_key="measure_time",
|
||||||
@ -195,12 +144,6 @@ async def async_setup_entry(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _to_datetime(measuretime: str) -> datetime:
|
|
||||||
"""Return isoformatted utc time."""
|
|
||||||
time_obj = datetime.strptime(measuretime, "%Y-%m-%dT%H:%M:%S.%f%z")
|
|
||||||
return as_utc(time_obj)
|
|
||||||
|
|
||||||
|
|
||||||
class TrafikverketWeatherStation(
|
class TrafikverketWeatherStation(
|
||||||
CoordinatorEntity[TVDataUpdateCoordinator], SensorEntity
|
CoordinatorEntity[TVDataUpdateCoordinator], SensorEntity
|
||||||
):
|
):
|
||||||
@ -246,10 +189,3 @@ class TrafikverketWeatherStation(
|
|||||||
if state is None and self.entity_description.key in NONE_IS_ZERO_SENSORS:
|
if state is None and self.entity_description.key in NONE_IS_ZERO_SENSORS:
|
||||||
return 0
|
return 0
|
||||||
return state
|
return state
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self) -> bool:
|
|
||||||
"""Return if entity is available."""
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
assert self.coordinator.data.active
|
|
||||||
return self.coordinator.data.active and super().available
|
|
||||||
|
@ -35,56 +35,20 @@
|
|||||||
"precipitation": {
|
"precipitation": {
|
||||||
"name": "Precipitation type",
|
"name": "Precipitation type",
|
||||||
"state": {
|
"state": {
|
||||||
"drizzle": "Drizzle",
|
"no": "No",
|
||||||
"hail": "Hail",
|
|
||||||
"none": "None",
|
|
||||||
"rain": "Rain",
|
"rain": "Rain",
|
||||||
|
"freezing_rain": "Freezing rain",
|
||||||
"snow": "Snow",
|
"snow": "Snow",
|
||||||
"rain_snow_mixed": "Rain and snow mixed",
|
"sleet": "Sleet",
|
||||||
"freezing_rain": "Freezing rain"
|
"yes": "Yes"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"wind_direction": {
|
"wind_direction": {
|
||||||
"name": "Wind direction"
|
"name": "Wind direction"
|
||||||
},
|
},
|
||||||
"wind_direction_text": {
|
|
||||||
"name": "Wind direction text",
|
|
||||||
"state": {
|
|
||||||
"east": "East",
|
|
||||||
"north_east": "North east",
|
|
||||||
"east_south_east": "East-south east",
|
|
||||||
"north": "North",
|
|
||||||
"north_north_east": "North-north east",
|
|
||||||
"north_north_west": "North-north west",
|
|
||||||
"north_west": "North west",
|
|
||||||
"south": "South",
|
|
||||||
"south_east": "South east",
|
|
||||||
"south_south_west": "South-south west",
|
|
||||||
"south_west": "South west",
|
|
||||||
"west": "West"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"wind_speed_max": {
|
"wind_speed_max": {
|
||||||
"name": "Wind speed max"
|
"name": "Wind speed max"
|
||||||
},
|
},
|
||||||
"precipitation_amountname": {
|
|
||||||
"name": "Precipitation name",
|
|
||||||
"state": {
|
|
||||||
"error": "Error",
|
|
||||||
"mild_rain": "Mild rain",
|
|
||||||
"moderate_rain": "Moderate rain",
|
|
||||||
"heavy_rain": "Heavy rain",
|
|
||||||
"mild_snow_rain": "Mild rain and snow mixed",
|
|
||||||
"moderate_snow_rain": "Moderate rain and snow mixed",
|
|
||||||
"heavy_snow_rain": "Heavy rain and snow mixed",
|
|
||||||
"mild_snow": "Mild snow",
|
|
||||||
"moderate_snow": "Moderate snow",
|
|
||||||
"heavy_snow": "Heavy snow",
|
|
||||||
"other": "Other",
|
|
||||||
"none": "None",
|
|
||||||
"unknown": "Unknown"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"measure_time": {
|
"measure_time": {
|
||||||
"name": "Measure time"
|
"name": "Measure time"
|
||||||
}
|
}
|
||||||
|
@ -2234,7 +2234,7 @@ pytradfri[async]==9.0.1
|
|||||||
# homeassistant.components.trafikverket_ferry
|
# homeassistant.components.trafikverket_ferry
|
||||||
# homeassistant.components.trafikverket_train
|
# homeassistant.components.trafikverket_train
|
||||||
# homeassistant.components.trafikverket_weatherstation
|
# homeassistant.components.trafikverket_weatherstation
|
||||||
pytrafikverket==0.3.8
|
pytrafikverket==0.3.9.1
|
||||||
|
|
||||||
# homeassistant.components.v2c
|
# homeassistant.components.v2c
|
||||||
pytrydan==0.4.0
|
pytrydan==0.4.0
|
||||||
|
@ -1664,7 +1664,7 @@ pytradfri[async]==9.0.1
|
|||||||
# homeassistant.components.trafikverket_ferry
|
# homeassistant.components.trafikverket_ferry
|
||||||
# homeassistant.components.trafikverket_train
|
# homeassistant.components.trafikverket_train
|
||||||
# homeassistant.components.trafikverket_weatherstation
|
# homeassistant.components.trafikverket_weatherstation
|
||||||
pytrafikverket==0.3.8
|
pytrafikverket==0.3.9.1
|
||||||
|
|
||||||
# homeassistant.components.v2c
|
# homeassistant.components.v2c
|
||||||
pytrydan==0.4.0
|
pytrydan==0.4.0
|
||||||
|
@ -6,7 +6,6 @@ from unittest.mock import patch
|
|||||||
import pytest
|
import pytest
|
||||||
from pytrafikverket.exceptions import (
|
from pytrafikverket.exceptions import (
|
||||||
InvalidAuthentication,
|
InvalidAuthentication,
|
||||||
MultipleTrainAnnouncementFound,
|
|
||||||
MultipleTrainStationsFound,
|
MultipleTrainStationsFound,
|
||||||
NoTrainAnnouncementFound,
|
NoTrainAnnouncementFound,
|
||||||
NoTrainStationFound,
|
NoTrainStationFound,
|
||||||
@ -177,10 +176,6 @@ async def test_flow_fails(
|
|||||||
NoTrainAnnouncementFound,
|
NoTrainAnnouncementFound,
|
||||||
"no_trains",
|
"no_trains",
|
||||||
),
|
),
|
||||||
(
|
|
||||||
MultipleTrainAnnouncementFound,
|
|
||||||
"multiple_trains",
|
|
||||||
),
|
|
||||||
(
|
(
|
||||||
UnknownError,
|
UnknownError,
|
||||||
"cannot_connect",
|
"cannot_connect",
|
||||||
@ -371,10 +366,6 @@ async def test_reauth_flow_error(
|
|||||||
NoTrainAnnouncementFound,
|
NoTrainAnnouncementFound,
|
||||||
"no_trains",
|
"no_trains",
|
||||||
),
|
),
|
||||||
(
|
|
||||||
MultipleTrainAnnouncementFound,
|
|
||||||
"multiple_trains",
|
|
||||||
),
|
|
||||||
(
|
(
|
||||||
UnknownError,
|
UnknownError,
|
||||||
"cannot_connect",
|
"cannot_connect",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user