Skip to content

Commit

Permalink
Added simple sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramunas-OAG committed Jul 23, 2024
1 parent 04791db commit 2c0e7ee
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Meteo.LT integration for Home Assistant
<img width="90" height="90" src="https://github.com/Brunas/meteo_lt/blob/main/images/icon.png?raw=true" style="float: left; margin-right: 20px; margin-top: 10px;" >

Home Assistant integration for Meteo.Lt REST API

[![GitHub Release][releases-shield]][releases]
Expand All @@ -8,9 +10,12 @@ Home Assistant integration for Meteo.Lt REST API

<a href="https://buymeacoffee.com/pdfdc52z8h" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 40px !important;width: 145px !important;" ></a>

<img width="90" height="90" src="https://github.com/Brunas/meteo_lt/blob/main/images/icon.png?raw=true" style="float: left; margin-right: 20px; margin-top: 10px;" >
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.

Expand Down
2 changes: 1 addition & 1 deletion custom_components/meteo_lt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
59 changes: 59 additions & 0 deletions custom_components/meteo_lt/sensor.py
Original file line number Diff line number Diff line change
@@ -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,
}

0 comments on commit 2c0e7ee

Please sign in to comment.