From 2c0e7eea845da6bc2fd658e3717c584ea05a26e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C5=ABnas=20Bunokas?= Date: Tue, 23 Jul 2024 17:23:08 +0300 Subject: [PATCH] Added simple sensor --- README.md | 11 +++-- custom_components/meteo_lt/__init__.py | 2 +- custom_components/meteo_lt/sensor.py | 59 ++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 custom_components/meteo_lt/sensor.py diff --git a/README.md b/README.md index f99c5d5..ba44da0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ # Meteo.LT integration for Home Assistant + + Home Assistant integration for Meteo.Lt REST API [![GitHub Release][releases-shield]][releases] @@ -8,9 +10,12 @@ Home Assistant integration for Meteo.Lt REST API Buy Me A Coffee - -This integration adds support for retrieving the Forecast data from [Api.Meteo.Lt](https://api.meteo.lt). -This integration will set up the Home Assistant `weather` entity, with current data, and hourly forecast data. The first Forecast data record is treated as current data. +This integration adds support for retrieving the Forecast data from [Api.Meteo.Lt](https://api.meteo.lt) and setting up following platforms in Home Assistant: + +Platform | Description +-- | -- +`weather` | A Home Assistant `weather` entity, with current data, and hourly forecast data. The first forecast record is treated as current data. +`sensor` | A Home Assistant `sensor` entity, with all available data taken from the forecast first record. Minimum required version of Home Assistant is **2024.7.3** as this integration uses the new Weather entity forecast types and it does **not** create Forecast Attributes. diff --git a/custom_components/meteo_lt/__init__.py b/custom_components/meteo_lt/__init__.py index 8268309..eaf4620 100644 --- a/custom_components/meteo_lt/__init__.py +++ b/custom_components/meteo_lt/__init__.py @@ -11,7 +11,7 @@ from .const import DOMAIN, LOGGER from .coordinator import MeteoLtCoordinator -PLATFORMS: Final = [Platform.WEATHER] +PLATFORMS: Final = [Platform.WEATHER, Platform.SENSOR] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Meteo.Lt from a config entry.""" diff --git a/custom_components/meteo_lt/sensor.py b/custom_components/meteo_lt/sensor.py new file mode 100644 index 0000000..9606d69 --- /dev/null +++ b/custom_components/meteo_lt/sensor.py @@ -0,0 +1,59 @@ +"""sensor.py""" + +from homeassistant.components.sensor import SensorEntity +from homeassistant.const import ( + UnitOfSpeed, + UnitOfTemperature, + UnitOfPressure, + UnitOfPrecipitationDepth, +) +from homeassistant.helpers.update_coordinator import CoordinatorEntity +from .const import DOMAIN, LOGGER + +async def async_setup_entry(hass, entry, async_add_entities): + """Set up Meteo LT sensors based on a config entry.""" + coordinator = hass.data[DOMAIN][entry.entry_id]["coordinator"] + nearest_place = hass.data[DOMAIN][entry.entry_id]["nearest_place"] + async_add_entities([MeteoLtCurrentConditionsSensor(coordinator, nearest_place)]) + +class MeteoLtCurrentConditionsSensor(CoordinatorEntity, SensorEntity): + """Representation of a Meteo.Lt Current Conditions Sensor.""" + + def __init__(self, coordinator, nearest_place): + """Initialize the sensor.""" + super().__init__(coordinator) + self._name = f"Meteo.Lt {nearest_place.name} - Current Conditions" + self._state = None + + @property + def name(self): + """Return the name of the sensor.""" + return self._name + + @property + def state(self): + """Return the state of the sensor.""" + return self.coordinator.data.current_conditions().temperature + + @property + def extra_state_attributes(self): + """Return the state attributes.""" + current_conditions = self.coordinator.data.current_conditions() + LOGGER.debug(f"Current conditions: %s", current_conditions) + + return { + "native_temperature": current_conditions.temperature, + "native_apparent_temperature": current_conditions.apparent_temperature, + "native_wind_speed": current_conditions.wind_speed, + "native_wind_gust_speed": current_conditions.wind_gust_speed, + "wind_bearing": current_conditions.wind_bearing, + "cloud_coverage": current_conditions.cloud_coverage, + "native_pressure": current_conditions.pressure, + "humidity": current_conditions.humidity, + "native_precipitation": current_conditions.precipitation, + "condition": current_conditions.condition, + "native_temperature_unit": UnitOfTemperature.CELSIUS, + "native_wind_speed_unit":UnitOfSpeed.METERS_PER_SECOND, + "native_pressure_unit": UnitOfPressure.HPA, + "native_precipitation_unit": UnitOfPrecipitationDepth.MILLIMETERS, + }