mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +00:00
Use HassKey in weather (#126329)
This commit is contained in:
parent
d40464e5d3
commit
1b4ba68e18
@ -63,6 +63,7 @@ from .const import ( # noqa: F401
|
|||||||
ATTR_WEATHER_WIND_SPEED,
|
ATTR_WEATHER_WIND_SPEED,
|
||||||
ATTR_WEATHER_WIND_SPEED_UNIT,
|
ATTR_WEATHER_WIND_SPEED_UNIT,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
DOMAIN_DATA,
|
||||||
INTENT_GET_WEATHER,
|
INTENT_GET_WEATHER,
|
||||||
UNIT_CONVERSIONS,
|
UNIT_CONVERSIONS,
|
||||||
VALID_UNITS,
|
VALID_UNITS,
|
||||||
@ -196,7 +197,7 @@ class Forecast(TypedDict, total=False):
|
|||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Set up the weather component."""
|
"""Set up the weather component."""
|
||||||
component = hass.data[DOMAIN] = EntityComponent[WeatherEntity](
|
component = hass.data[DOMAIN_DATA] = EntityComponent[WeatherEntity](
|
||||||
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
|
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
|
||||||
)
|
)
|
||||||
component.async_register_entity_service(
|
component.async_register_entity_service(
|
||||||
@ -217,14 +218,12 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up a config entry."""
|
"""Set up a config entry."""
|
||||||
component: EntityComponent[WeatherEntity] = hass.data[DOMAIN]
|
return await hass.data[DOMAIN_DATA].async_setup_entry(entry)
|
||||||
return await component.async_setup_entry(entry)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
component: EntityComponent[WeatherEntity] = hass.data[DOMAIN]
|
return await hass.data[DOMAIN_DATA].async_unload_entry(entry)
|
||||||
return await component.async_unload_entry(entry)
|
|
||||||
|
|
||||||
|
|
||||||
class WeatherEntityDescription(EntityDescription, frozen_or_thawed=True):
|
class WeatherEntityDescription(EntityDescription, frozen_or_thawed=True):
|
||||||
|
@ -4,7 +4,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from enum import IntFlag
|
from enum import IntFlag
|
||||||
from typing import Final
|
from typing import TYPE_CHECKING, Final
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
UnitOfLength,
|
UnitOfLength,
|
||||||
@ -13,6 +13,7 @@ from homeassistant.const import (
|
|||||||
UnitOfSpeed,
|
UnitOfSpeed,
|
||||||
UnitOfTemperature,
|
UnitOfTemperature,
|
||||||
)
|
)
|
||||||
|
from homeassistant.util.hass_dict import HassKey
|
||||||
from homeassistant.util.unit_conversion import (
|
from homeassistant.util.unit_conversion import (
|
||||||
DistanceConverter,
|
DistanceConverter,
|
||||||
PressureConverter,
|
PressureConverter,
|
||||||
@ -20,6 +21,11 @@ from homeassistant.util.unit_conversion import (
|
|||||||
TemperatureConverter,
|
TemperatureConverter,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
|
|
||||||
|
from . import WeatherEntity
|
||||||
|
|
||||||
|
|
||||||
class WeatherEntityFeature(IntFlag):
|
class WeatherEntityFeature(IntFlag):
|
||||||
"""Supported features of the update entity."""
|
"""Supported features of the update entity."""
|
||||||
@ -48,6 +54,7 @@ ATTR_WEATHER_CLOUD_COVERAGE = "cloud_coverage"
|
|||||||
ATTR_WEATHER_UV_INDEX = "uv_index"
|
ATTR_WEATHER_UV_INDEX = "uv_index"
|
||||||
|
|
||||||
DOMAIN: Final = "weather"
|
DOMAIN: Final = "weather"
|
||||||
|
DOMAIN_DATA: HassKey[EntityComponent[WeatherEntity]] = HassKey(DOMAIN)
|
||||||
|
|
||||||
INTENT_GET_WEATHER = "HassGetWeather"
|
INTENT_GET_WEATHER = "HassGetWeather"
|
||||||
|
|
||||||
|
@ -9,10 +9,9 @@ import voluptuous as vol
|
|||||||
from homeassistant.components import websocket_api
|
from homeassistant.components import websocket_api
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
|
||||||
from homeassistant.util.json import JsonValueType
|
from homeassistant.util.json import JsonValueType
|
||||||
|
|
||||||
from .const import DOMAIN, VALID_UNITS, WeatherEntityFeature
|
from .const import DOMAIN, DOMAIN_DATA, VALID_UNITS, WeatherEntityFeature
|
||||||
|
|
||||||
FORECAST_TYPE_TO_FLAG = {
|
FORECAST_TYPE_TO_FLAG = {
|
||||||
"daily": WeatherEntityFeature.FORECAST_DAILY,
|
"daily": WeatherEntityFeature.FORECAST_DAILY,
|
||||||
@ -56,13 +55,10 @@ async def ws_subscribe_forecast(
|
|||||||
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
|
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Subscribe to weather forecasts."""
|
"""Subscribe to weather forecasts."""
|
||||||
from . import WeatherEntity # pylint: disable=import-outside-toplevel
|
|
||||||
|
|
||||||
component: EntityComponent[WeatherEntity] = hass.data[DOMAIN]
|
|
||||||
entity_id: str = msg["entity_id"]
|
entity_id: str = msg["entity_id"]
|
||||||
forecast_type: Literal["daily", "hourly", "twice_daily"] = msg["forecast_type"]
|
forecast_type: Literal["daily", "hourly", "twice_daily"] = msg["forecast_type"]
|
||||||
|
|
||||||
if not (entity := component.get_entity(msg["entity_id"])):
|
if not (entity := hass.data[DOMAIN_DATA].get_entity(msg["entity_id"])):
|
||||||
connection.send_error(
|
connection.send_error(
|
||||||
msg["id"],
|
msg["id"],
|
||||||
"invalid_entity_id",
|
"invalid_entity_id",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user