Skip to content

Commit

Permalink
Added properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjarne Riis committed Sep 18, 2023
1 parent 11a6631 commit 69548b0
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 2 deletions.
46 changes: 46 additions & 0 deletions custom_components/weatherflow_forecast/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,52 @@
"""Constants for WeatherFlow Forecast component."""

from homeassistant.components.weather import (
ATTR_CONDITION_CLEAR_NIGHT,
ATTR_CONDITION_CLOUDY,
ATTR_CONDITION_FOG,
ATTR_CONDITION_LIGHTNING_RAINY,
ATTR_CONDITION_PARTLYCLOUDY,
ATTR_CONDITION_POURING,
ATTR_CONDITION_RAINY,
ATTR_CONDITION_SNOWY,
ATTR_CONDITION_SNOWY_RAINY,
ATTR_CONDITION_SUNNY,
ATTR_FORECAST_CLOUD_COVERAGE,
ATTR_FORECAST_CONDITION,
ATTR_FORECAST_HUMIDITY,
ATTR_FORECAST_NATIVE_PRECIPITATION,
ATTR_FORECAST_NATIVE_TEMP,
ATTR_FORECAST_NATIVE_TEMP_LOW,
ATTR_FORECAST_NATIVE_WIND_GUST_SPEED,
ATTR_FORECAST_NATIVE_WIND_SPEED,
ATTR_FORECAST_PRECIPITATION_PROBABILITY,
ATTR_FORECAST_TIME,
ATTR_FORECAST_WIND_BEARING,
ATTR_WEATHER_CLOUD_COVERAGE,
ATTR_WEATHER_DEW_POINT,
ATTR_WEATHER_HUMIDITY,
ATTR_WEATHER_PRESSURE,
ATTR_WEATHER_TEMPERATURE,
ATTR_WEATHER_VISIBILITY,
ATTR_WEATHER_WIND_BEARING,
ATTR_WEATHER_WIND_GUST_SPEED,
ATTR_WEATHER_WIND_SPEED,
DOMAIN as WEATHER_DOMAIN,
)

DOMAIN = "weatherflow_forecast"

CONF_API_TOKEN = "api_token"
CONF_STATION_ID = "station_id"

ATTR_MAP = {
ATTR_WEATHER_HUMIDITY: "humidity",
ATTR_WEATHER_PRESSURE: "pressure",
ATTR_WEATHER_TEMPERATURE: "temperature",
ATTR_WEATHER_VISIBILITY: "visibility",
ATTR_WEATHER_WIND_BEARING: "wind_bearing",
ATTR_WEATHER_WIND_SPEED: "wind_speed",
ATTR_WEATHER_WIND_GUST_SPEED: "wind_gust",
ATTR_WEATHER_CLOUD_COVERAGE: "cloudiness",
ATTR_WEATHER_DEW_POINT: "dew_point",
}
112 changes: 110 additions & 2 deletions custom_components/weatherflow_forecast/weather.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Support for WeatherFlow Forecast weather service."""
from __future__ import annotations

from typing import Any
from types import MappingProxyType
from typing import TYPE_CHECKING, Any

from homeassistant.components.weather import (
ATTR_FORECAST_CONDITION,
Expand Down Expand Up @@ -33,7 +34,7 @@
from homeassistant.util.unit_system import METRIC_SYSTEM

from . import WeatherFlowForecastDataUpdateCoordinator
from .const import DOMAIN, CONF_API_TOKEN, CONF_STATION_ID
from .const import ATTR_MAP, DOMAIN, CONF_API_TOKEN, CONF_STATION_ID

DEFAULT_NAME = "WeatherFlow Forecast"

Expand All @@ -46,11 +47,36 @@ async def async_setup_entry(
coordinator: WeatherFlowForecastDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
entity_registry = er.async_get(hass)

name: str | None
is_metric = hass.config.units is METRIC_SYSTEM

if (name := config_entry.data.get(CONF_NAME)) and name is None:
name = DEFAULT_NAME
elif TYPE_CHECKING:
assert isinstance(name, str)

entities = [WeatherFlowWeather(coordinator, config_entry.data,
hass.config.units is METRIC_SYSTEM, False)]

# Add hourly entity to legacy config entries
if entity_registry.async_get_entity_id(
WEATHER_DOMAIN,
DOMAIN,
_calculate_unique_id(config_entry.data, True)
):
name = f"{name} hourly"
entities.append(WeatherFlowWeather(coordinator, config_entry.data, True, name, is_metric))

async_add_entities(entities)

def _calculate_unique_id(config: MappingProxyType[str, Any], hourly: bool) -> str:
"""Calculate unique ID."""
name_appendix = ""
if hourly:
name_appendix = "-hourly"

return f"{config[CONF_STATION_ID]}{name_appendix}"

class WeatherFlowWeather(SingleCoordinatorWeatherEntity[WeatherFlowForecastDataUpdateCoordinator]):
"""Implementation of a WeatherFlow weather condition."""

Expand All @@ -65,3 +91,85 @@ class WeatherFlowWeather(SingleCoordinatorWeatherEntity[WeatherFlowForecastDataU
_attr_supported_features = (
WeatherEntityFeature.FORECAST_DAILY | WeatherEntityFeature.FORECAST_HOURLY
)

def __init__(
self,
coordinator: WeatherFlowForecastDataUpdateCoordinator,
config: MappingProxyType[str, Any],
hourly: bool,
name: str,
is_metric: bool,
) -> None:
"""Initialise the platform with a data instance and station."""
super().__init__(coordinator)
self._attr_unique_id = _calculate_unique_id(config, hourly)
self._config = config
self._is_metric = is_metric
self._hourly = hourly
self._attr_entity_registry_enabled_default = not hourly
self._attr_device_info = DeviceInfo(
name = "Forecast",
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN,)}, # type: ignore[arg-type]
manufacturer="WeatherFlow",
model="Forecast",
configuration_url="https://weatherflow.github.io/Tempest/api/",
)
self._attr_name = name

@property
def condition(self) -> str | None:
"""Return the current condition."""
condition = self.coordinator.data.current_weather_data.get("condition")
if condition is None:
return None
return condition

@property
def native_temperature(self) -> float | None:
"""Return the temperature."""
return self.coordinator.data.current_weather_data.get(
ATTR_MAP[ATTR_WEATHER_TEMPERATURE]
)

@property
def native_pressure(self) -> float | None:
"""Return the pressure."""
return self.coordinator.data.current_weather_data.get(
ATTR_MAP[ATTR_WEATHER_PRESSURE]
)

@property
def humidity(self) -> float | None:
"""Return the humidity."""
return self.coordinator.data.current_weather_data.get(
ATTR_MAP[ATTR_WEATHER_HUMIDITY]
)

@property
def native_wind_speed(self) -> float | None:
"""Return the wind speed."""
return self.coordinator.data.current_weather_data.get(
ATTR_MAP[ATTR_WEATHER_WIND_SPEED]
)

@property
def wind_bearing(self) -> float | str | None:
"""Return the wind direction."""
return self.coordinator.data.current_weather_data.get(
ATTR_MAP[ATTR_WEATHER_WIND_BEARING]
)

@property
def native_wind_gust_speed(self) -> float | None:
"""Return the wind gust speed in native units."""
return self.coordinator.data.current_weather_data.get(
ATTR_MAP[ATTR_WEATHER_WIND_GUST_SPEED]
)

@property
def native_dew_point(self) -> float | None:
"""Return the dew point."""
return self.coordinator.data.current_weather_data.get(
ATTR_MAP[ATTR_WEATHER_DEW_POINT]
)

0 comments on commit 69548b0

Please sign in to comment.