Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/devcontainer improvement #10

Merged
merged 5 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions .devcontainer/Dockerfile

This file was deleted.

15 changes: 5 additions & 10 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{
"name": "Home Assistant Dev Container",
"build": {
"dockerfile": "Dockerfile",
"context": ".."
},
"postCreateCommand": "python -m pip install -r requirements.txt",
"image": "homeassistant/home-assistant:stable",
"runArgs": [
"-e",
"GIT_EDTIOR='code --wait'"
Expand Down Expand Up @@ -38,15 +34,14 @@
"files.trimTrailingWhitespace": true
}
},
"remoteUser": "vscode",
"mounts": [
"source=${localWorkspaceFolder}/config,target=/config,type=bind",
"source=${localWorkspaceFolder}/custom_components,target=/config/custom_components,type=bind"
"source=${localWorkspaceFolder}/custom_components,target=/root/.homeassistant/custom_components,type=bind"
],
"forwardPorts": [
8123
8123
],
"remoteEnv": {
"TZ": "Europe/Vilnius"
}
},
"postCreateCommand": "pip install -r requirements.txt && hass"
}
1 change: 0 additions & 1 deletion configuration.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion custom_components/meteo_lt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
LOGGER.debug("Nearest place found: %s", nearest_place)

coordinator = MeteoLtCoordinator(hass, api, nearest_place)
await coordinator.async_refresh()
await coordinator.async_config_entry_first_refresh()

entry.async_on_unload(entry.add_update_listener(_async_update_listener))
hass.data[DOMAIN][entry.entry_id] = {
Expand Down
5 changes: 4 additions & 1 deletion custom_components/meteo_lt/coordinator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""coordinator.py"""

from datetime import timedelta
from datetime import datetime, timedelta
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

from .const import MANUFACTURER, LOGGER, UPDATE_MINUTES
Expand All @@ -13,15 +13,18 @@ def __init__(self, hass, api, nearest_place):
"""Initialize."""
self.api = api
self.nearest_place = nearest_place
self.last_updated = None
super().__init__(
hass,
LOGGER,
name=MANUFACTURER,
update_interval=timedelta(minutes=UPDATE_MINUTES),
always_update=True,
)

async def _async_update_data(self):
"""Fetch data from API."""
forecast = await self.api.get_forecast(self.nearest_place.code)
LOGGER.debug("Forecast retrieved: %s", forecast)
self.last_updated = datetime.now().isoformat()
return forecast
22 changes: 18 additions & 4 deletions custom_components/meteo_lt/sensor.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""sensor.py"""

from functools import cached_property
from typing import Any
from typing import Dict, Any
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import (
UnitOfSpeed,
UnitOfTemperature,
UnitOfPressure,
UnitOfPrecipitationDepth,
)
from homeassistant.core import callback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN, LOGGER

Expand Down Expand Up @@ -43,13 +43,13 @@ def __init__(self, coordinator, nearest_place, config_entry):
self._attr_unique_id = f"{config_entry.entry_id}-sensor"
self._state = None

@cached_property
@property
def native_value(self):
"""Return the value of the sensor."""
return self.coordinator.data.current_conditions().temperature

@property
def extra_state_attributes(self) -> dict[str, Any] | None:
def extra_state_attributes(self) -> Dict[str, Any] | None:
"""Return the state attributes."""
current_conditions = self.coordinator.data.current_conditions()
LOGGER.debug("Current conditions: %s", current_conditions)
Expand All @@ -69,4 +69,18 @@ def extra_state_attributes(self) -> dict[str, Any] | None:
"native_wind_speed_unit": UnitOfSpeed.METERS_PER_SECOND,
"native_pressure_unit": UnitOfPressure.HPA,
"native_precipitation_unit": UnitOfPrecipitationDepth.MILLIMETERS,
"last_updated": self.coordinator.last_updated,
}

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
LOGGER.debug(
"Handling Meteo.Lt sensor coordinator update for entity %s", self.entity_id
)
self.async_write_ha_state()

async def async_update(self):
"""Fetch new state data for the sensor."""
LOGGER.debug("Updating Meteo.Lt sensor entity %s", self.entity_id)
await self.coordinator.async_request_refresh()
54 changes: 32 additions & 22 deletions custom_components/meteo_lt/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# pylint: disable=unused-argument, abstract-method

from functools import cached_property
from typing import List, Dict, Union
from homeassistant.components.weather import WeatherEntity, WeatherEntityFeature
from homeassistant.config_entries import ConfigEntry
Expand All @@ -12,7 +11,7 @@
UnitOfPressure,
UnitOfPrecipitationDepth,
)
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN, MANUFACTURER, LOGGER
Expand Down Expand Up @@ -47,83 +46,79 @@ def __init__(self, coordinator, nearest_place, config_entry):
super().__init__(coordinator)
self._attr_name = f"{config_entry.title} {nearest_place.name}"
self._attr_unique_id = config_entry.entry_id

@property
def device_info(self):
"""device info"""
return {
self._attr_device_info = {
"entry_type": DeviceEntryType.SERVICE,
"identifiers": {(DOMAIN, self._attr_unique_id)},
"name": self._attr_name,
"manufacturer": MANUFACTURER,
}

@cached_property
@property
def native_temperature(self):
"""Native temperature"""
return self.coordinator.data.forecast_timestamps[0].temperature

@cached_property
@property
def native_temperature_unit(self):
"""Native temperature unit"""
return UnitOfTemperature.CELSIUS

@cached_property
@property
def humidity(self):
"""Humidity"""
return self.coordinator.data.forecast_timestamps[0].humidity

@cached_property
@property
def native_wind_speed(self):
"""Native wind speed"""
return self.coordinator.data.forecast_timestamps[0].wind_speed

@cached_property
@property
def native_wind_speed_unit(self):
"""Native wind speed unit"""
return UnitOfSpeed.METERS_PER_SECOND

@cached_property
@property
def wind_bearing(self):
"""Native wind bearing"""
return self.coordinator.data.forecast_timestamps[0].wind_bearing

@cached_property
@property
def native_pressure(self):
"""Native pressure"""
return self.coordinator.data.forecast_timestamps[0].pressure

@cached_property
@property
def native_pressure_unit(self):
"""Native pressure unit"""
return UnitOfPressure.HPA

@cached_property
@property
def native_precipitation(self):
"""Native precipitation"""
return self.coordinator.data.forecast_timestamps[0].precipitation

@cached_property
@property
def native_precipitation_unit(self):
"""Native precipitation unit"""
return UnitOfPrecipitationDepth.MILLIMETERS

@cached_property
@property
def condition(self):
"""Condition"""
return self.coordinator.data.forecast_timestamps[0].condition

@cached_property
@property
def cloud_coverage(self):
"""Cloud coverage"""
return self.coordinator.data.forecast_timestamps[0].cloud_coverage

@cached_property
@property
def native_apparent_temperature(self):
"""Native apparent temperature"""
return self.coordinator.data.forecast_timestamps[0].apparent_temperature

@cached_property
@property
def native_wind_gust_speed(self):
"""Native wind gust speed"""
return self.coordinator.data.forecast_timestamps[0].wind_gust_speed
Expand All @@ -133,6 +128,13 @@ def supported_features(self):
"""Return the list of supported features."""
return WeatherEntityFeature.FORECAST_HOURLY

@property
def extra_state_attributes(self):
"""Return extra attributes."""
return {
"last_updated": self.coordinator.last_updated,
}

async def async_forecast_hourly(
self,
) -> Union[List[Dict[str, Union[str, float]]], None]:
Expand Down Expand Up @@ -163,7 +165,15 @@ async def async_forecast_hourly(
LOGGER.debug("Hourly_forecast created: %s", hourly_forecast)
return hourly_forecast

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
LOGGER.debug(
"Handling Meteo.Lt weather coordinator update for entity %s", self.entity_id
)
self.async_write_ha_state()

async def async_update(self):
"""Refreshing coordinator"""
LOGGER.debug("Updating MeteoLtWeather entity.")
LOGGER.debug("Updating Meteo.Lt weather entity %s", self.entity_id)
await self.coordinator.async_request_refresh()