Skip to content

Commit

Permalink
device_info, unique_id and (re/un)loading change
Browse files Browse the repository at this point in the history
  • Loading branch information
Brunas committed Jul 23, 2024
1 parent 2c0e7ee commit 2f9f7d8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
12 changes: 5 additions & 7 deletions custom_components/meteo_lt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

return True

async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update options."""
await hass.config_entries.async_reload(entry.entry_id)

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
LOGGER.info("Unloading Meteo.Lt config entry")

unload_ok = await hass.config_entries.async_forward_entry_unload(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)

return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update listener."""
Expand Down
7 changes: 5 additions & 2 deletions custom_components/meteo_lt/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@
UnitOfPressure,
UnitOfPrecipitationDepth,
)
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
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."""
LOGGER.debug("Sensor setting up config entry %s", 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)])
async_add_entities([MeteoLtCurrentConditionsSensor(coordinator, nearest_place, entry)])

class MeteoLtCurrentConditionsSensor(CoordinatorEntity, SensorEntity):
"""Representation of a Meteo.Lt Current Conditions Sensor."""

def __init__(self, coordinator, nearest_place):
def __init__(self, coordinator, nearest_place, config_entry):
"""Initialize the sensor."""
super().__init__(coordinator)
self._name = f"Meteo.Lt {nearest_place.name} - Current Conditions"
self._state = None
self._attr_unique_id = f"{config_entry.unique_id}-sensor"

@property
def name(self):
Expand Down
18 changes: 15 additions & 3 deletions custom_components/meteo_lt/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,43 @@
UnitOfPrecipitationDepth,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN, LOGGER


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities):
"""Set up Meteo.Lt weather based on a config entry."""
LOGGER.debug("Setting up config entry")
LOGGER.debug("Weather Setting up config entry %s", entry)
coordinator = hass.data[DOMAIN][entry.entry_id]["coordinator"]
nearest_place = hass.data[DOMAIN][entry.entry_id]["nearest_place"]
async_add_entities([MeteoLtWeather(coordinator, nearest_place)], True)
async_add_entities([MeteoLtWeather(coordinator, nearest_place, entry)], True)


class MeteoLtWeather(CoordinatorEntity, WeatherEntity):
"""Meteo.lt WeatherEntity implementation"""

def __init__(self, coordinator, nearest_place):
def __init__(self, coordinator, nearest_place, config_entry):
"""__init__"""
super().__init__(coordinator)
self._name = f"Meteo.Lt {nearest_place.name}"
self._attr_unique_id = config_entry.unique_id

@property
def name(self):
"""Name"""
return self._name

@property
def device_info(self):
"""device info"""
return {
"entry_type": DeviceEntryType.SERVICE,
"identifiers": {(DOMAIN, self._attr_unique_id)},
"name": self._name,
"manufacturer": "Meteo.Lt"
}

@cached_property
def native_temperature(self):
"""Native temperature"""
Expand Down

0 comments on commit 2f9f7d8

Please sign in to comment.