ha-2gis-weather/custom_components/two_gis_weather/weather.py
2025-11-04 11:21:01 +06:00

81 lines
2.7 KiB
Python

from __future__ import annotations
from typing import Any
from homeassistant.components.weather import (
WeatherEntity,
WeatherEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .coordinator import TwoGisWeatherUpdateCoordinator
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
coordinator: TwoGisWeatherUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities([TwoGisWeatherEntity(coordinator, entry)])
class TwoGisWeatherEntity(CoordinatorEntity[TwoGisWeatherUpdateCoordinator], WeatherEntity):
_attr_has_entity_name = True
_attr_name = None
_attr_supported_features = (
WeatherEntityFeature.FORECAST_DAILY | WeatherEntityFeature.FORECAST_HOURLY
)
def __init__(self, coordinator: TwoGisWeatherUpdateCoordinator, entry: ConfigEntry) -> None:
super().__init__(coordinator)
self._entry = entry
self._attr_unique_id = f"{entry.entry_id}_weather"
self._attr_name = entry.data.get("name") or "2GIS Weather"
@property
def native_temperature(self) -> float | None:
current = self.coordinator.data.get("current") if self.coordinator.data else None
return (current or {}).get("temperature")
@property
def native_temperature_unit(self) -> str:
return "°C"
@property
def condition(self) -> str | None:
current = self.coordinator.data.get("current") if self.coordinator.data else None
return (current or {}).get("condition")
@property
def native_wind_speed(self) -> float | None:
current = (self.coordinator.data or {}).get("current", {})
return current.get("wind_speed")
@property
def native_wind_speed_unit(self) -> str:
return "m/s"
@property
def wind_bearing(self) -> str | None:
current = (self.coordinator.data or {}).get("current", {})
return current.get("wind_bearing")
@property
def native_pressure(self) -> float | None:
current = (self.coordinator.data or {}).get("current", {})
return current.get("pressure")
@property
def native_pressure_unit(self) -> str:
return "hPa"
async def async_forecast_daily(self) -> list[dict[str, Any]] | None:
return (self.coordinator.data or {}).get("daily")
async def async_forecast_hourly(self) -> list[dict[str, Any]] | None:
return (self.coordinator.data or {}).get("hourly")