Skip to content

Commit

Permalink
Remaster all modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Limych committed May 12, 2024
1 parent 76cdfb6 commit 0011116
Show file tree
Hide file tree
Showing 21 changed files with 1,897 additions and 1,982 deletions.
15 changes: 3 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,12 @@ for today and 2 days forward.
gismeteo:
sweet_home:
sensors:
monitored_conditions:
- temperature
- humidity

dacha:
name: Our Country House
latitude: ...
longitude: ...
sensors:
monitored_conditions:
- temperature
- humidity
forecast_days: 2
```
Expand Down Expand Up @@ -118,14 +112,11 @@ I put a lot of work into making this repo and component available and updated to
> Setup for list of forecast sensors to display in the frontend.
>
> > **forecast_days:**\
> > _(positive int) (Optional) (Default: do not create any sensors)_\
> > _(positive int; 0–6) (Optional) (Default: do not create any sensors)_\
> > How many days ahead to make forecast sensors.\
> > **Note:** If you only need a forecast sensors for today, specify `0`.
> >
> > **monitored_conditions:**\
> > _(list) (Optional) (Default: None)_\
> > Conditions to display in the frontend.
> >


> > > **condition**\
> > > A human-readable text summary.
> > >
Expand Down
10 changes: 1 addition & 9 deletions config/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,11 @@ logger:
# debugpy:

gismeteo:
sweet_home:
weather: {}
sensors:
monitored_conditions:
- temperature
- humidity
sweet_home: {}

dacha:
name: Our Country House
latitude: 46.0561601
longitude: 13.1741225
sensors:
monitored_conditions:
- temperature
- humidity
forecast_days: 2
77 changes: 21 additions & 56 deletions custom_components/gismeteo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
https://github.com/Limych/ha-gismeteo/
"""

import asyncio
import logging

from aiohttp import ClientConnectorError
Expand All @@ -19,10 +18,10 @@
CONF_API_KEY,
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_MODE,
CONF_MONITORED_CONDITIONS,
CONF_NAME,
CONF_SENSORS,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
Expand All @@ -41,11 +40,7 @@
COORDINATOR,
DOMAIN,
DOMAIN_YAML,
FORECAST_MODE_DAILY,
FORECAST_MODE_HOURLY,
PLATFORMS,
SENSOR,
SENSOR_TYPES,
STARTUP_MESSAGE,
UNDO_UPDATE_LISTENER,
UPDATE_INTERVAL,
Expand All @@ -54,20 +49,12 @@
_LOGGER = logging.getLogger(__name__)


WEATHER_SCHEMA = vol.Schema(
{
vol.Optional(CONF_MODE, default=FORECAST_MODE_HOURLY): vol.In(
[FORECAST_MODE_HOURLY, FORECAST_MODE_DAILY]
),
}
)
forecast_days_int = vol.All(vol.Coerce(int), vol.Range(min=0, max=6))

SENSORS_SCHEMA = vol.Schema(
{
vol.Optional(CONF_FORECAST_DAYS): cv.positive_int,
vol.Optional(CONF_MONITORED_CONDITIONS, default=[]): vol.All(
cv.ensure_list, [vol.In(SENSOR_TYPES)]
),
vol.Optional(CONF_FORECAST_DAYS): forecast_days_int,
vol.Optional(CONF_MONITORED_CONDITIONS): cv.deprecated,
vol.Optional(CONF_FORECAST): cv.deprecated,
}
)
Expand Down Expand Up @@ -148,82 +135,60 @@ def _convert_yaml_config(config: ConfigType) -> ConfigType:
if CONF_SENSORS in cfg:
cfg.update(cfg[CONF_SENSORS])
cfg.pop(CONF_SENSORS)
cfg[CONF_PLATFORM_FORMAT.format(SENSOR)] = True
cfg[CONF_PLATFORM_FORMAT.format(Platform.SENSOR)] = True

return cfg


def _get_platforms(config: ConfigType) -> list[str]:
"""Get configured platforms."""
return [x for x in PLATFORMS if config.get(CONF_PLATFORM_FORMAT.format(x), True)]


async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Gismeteo as config entry."""
if config_entry.source == SOURCE_IMPORT:
if entry.source == SOURCE_IMPORT:
# Setup from configuration.yaml
platforms = set()

for uid, cfg in hass.data[DOMAIN_YAML].items():
cfg = _convert_yaml_config(cfg)

platforms.update(_get_platforms(cfg))

coordinator = await _async_get_coordinator(hass, uid, cfg)
hass.data[DOMAIN][uid] = {
COORDINATOR: coordinator,
}

undo_listener = config_entry.add_update_listener(update_listener)
hass.data[DOMAIN][config_entry.entry_id] = {
undo_listener = entry.add_update_listener(update_listener)
hass.data[DOMAIN][entry.entry_id] = {
UNDO_UPDATE_LISTENER: undo_listener,
}
platforms = list(platforms)

else:
# Setup from config entry
config = config_entry.data.copy() # type: ConfigType
config.update(config_entry.options)

platforms = _get_platforms(config)
config = entry.data.copy() # type: ConfigType
config.update(entry.options)

coordinator = await _async_get_coordinator(hass, config_entry.entry_id, config)
undo_listener = config_entry.add_update_listener(update_listener)
hass.data[DOMAIN][config_entry.entry_id] = {
coordinator = await _async_get_coordinator(hass, entry.entry_id, config)
undo_listener = entry.add_update_listener(update_listener)
hass.data[DOMAIN][entry.entry_id] = {
COORDINATOR: coordinator,
UNDO_UPDATE_LISTENER: undo_listener,
}

for component in platforms:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, component)
)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

return True


async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(config_entry, component)
for component in PLATFORMS
]
)
)
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

hass.data[DOMAIN][config_entry.entry_id][UNDO_UPDATE_LISTENER]()
hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENER]()

if unload_ok:
hass.data[DOMAIN].pop(config_entry.entry_id)
hass.data[DOMAIN].pop(entry.entry_id)

return unload_ok


async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry):
async def update_listener(hass: HomeAssistant, entry: ConfigEntry):
"""Update listener."""
await hass.config_entries.async_reload(config_entry.entry_id)
await hass.config_entries.async_reload(entry.entry_id)


class GismeteoDataUpdateCoordinator(DataUpdateCoordinator):
Expand Down
Loading

0 comments on commit 0011116

Please sign in to comment.