Skip to content

Commit 6edda71

Browse files
committed
Remaster configuration schema (#193)
1 parent 0be8617 commit 6edda71

File tree

15 files changed

+98
-146
lines changed

15 files changed

+98
-146
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,13 @@ for today and 2 days forward.
6868
# Example configuration.yaml entry
6969
gismeteo:
7070
sweet_home:
71-
sensors: {}
7271

7372
dacha:
7473
name: Our Country House
7574
latitude: ...
7675
longitude: ...
77-
sensors:
78-
forecast_days: 2
76+
add_sensors: yes
77+
forecast_days: 2
7978
```
8079
8180
See below detailed descriptions to configure component.
@@ -107,14 +106,15 @@ I put a lot of work into making this repo and component available and updated to
107106
> _(float) (Optional) (Default: coordinates from the Home Assistant configuration)_\
108107
> Longitude coordinate to monitor weather of (required if `latitude` is specified).
109108
>
110-
> **sensors:**\
111-
> _(list) (Optional)_\
112-
> Setup for list of forecast sensors to display in the frontend.
109+
> **add_sensors:**\
110+
> _(boolean) (Optional) (Default: false)_\
111+
> Enable this option to add current weather and forecast sensors to the frontend.
113112
>
114-
> > **forecast_days:**\
115-
> > _(positive int; 0–6) (Optional) (Default: do not create any sensors)_\
116-
> > How many days ahead to make forecast sensors.\
117-
> > **Note:** If you only need a forecast sensors for today, specify `0`.
113+
> **forecast_days:**\
114+
> _(positive int; 0–6) (Optional) (Default: do not create any sensors)_\
115+
> How many days ahead to make forecast sensors.\
116+
> **Note:** Forecast sensors will be created only if `add_sensors` option is enabled.\
117+
> **Note:** If you only need a forecast sensors for today, specify `0`.
118118

119119
When `sensors` option are enabled, it creates 20 sensors. Each shows one aspect of current weather. Only few basic sensors are enabled by default. But you can enable any sensor through device settings.
120120

config/configuration.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ logger:
1010

1111
gismeteo:
1212
sweet_home:
13-
sensors: {}
1413

1514
dacha:
1615
name: Our Country House

custom_components/gismeteo/__init__.py

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
CONF_API_KEY,
1919
CONF_LATITUDE,
2020
CONF_LONGITUDE,
21-
CONF_MONITORED_CONDITIONS,
2221
CONF_NAME,
2322
CONF_SENSORS,
24-
Platform,
2523
)
2624
from homeassistant.core import HomeAssistant
2725
from homeassistant.exceptions import ConfigEntryNotReady
@@ -33,10 +31,9 @@
3331

3432
from .api import ApiError, GismeteoApiClient
3533
from .const import (
34+
CONF_ADD_SENSORS,
3635
CONF_CACHE_DIR,
37-
CONF_FORECAST,
3836
CONF_FORECAST_DAYS,
39-
CONF_PLATFORM_FORMAT,
4037
COORDINATOR,
4138
DOMAIN,
4239
DOMAIN_YAML,
@@ -51,27 +48,22 @@
5148

5249
forecast_days_int = vol.All(vol.Coerce(int), vol.Range(min=0, max=6))
5350

54-
SENSORS_SCHEMA = vol.Schema(
55-
{
56-
vol.Optional(CONF_FORECAST_DAYS): forecast_days_int,
57-
vol.Optional(CONF_MONITORED_CONDITIONS): cv.deprecated,
58-
vol.Optional(CONF_FORECAST): cv.deprecated,
59-
}
60-
)
61-
6251
LOCATION_SCHEMA = vol.Schema(
6352
{
6453
vol.Optional(CONF_NAME): cv.string,
6554
vol.Optional(CONF_API_KEY): cv.string,
6655
vol.Optional(CONF_LATITUDE): cv.latitude,
6756
vol.Optional(CONF_LONGITUDE): cv.longitude,
68-
vol.Optional(CONF_SENSORS): SENSORS_SCHEMA,
57+
vol.Optional(CONF_SENSORS): cv.deprecated,
58+
vol.Optional(CONF_ADD_SENSORS, default=False): cv.boolean,
59+
vol.Optional(CONF_FORECAST_DAYS): forecast_days_int,
6960
vol.Optional(CONF_CACHE_DIR): cv.string,
7061
}
7162
)
7263

7364
CONFIG_SCHEMA = vol.Schema(
74-
{DOMAIN: cv.schema_with_slug_keys(LOCATION_SCHEMA)}, extra=vol.ALLOW_EXTRA
65+
{DOMAIN: cv.schema_with_slug_keys(vol.Any(LOCATION_SCHEMA, None))},
66+
extra=vol.ALLOW_EXTRA,
7567
)
7668

7769

@@ -99,8 +91,12 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
9991
return True
10092

10193

102-
def _get_api_client(hass: HomeAssistant, config: ConfigType) -> GismeteoApiClient:
94+
def _get_api_client(
95+
hass: HomeAssistant, config: ConfigType | None
96+
) -> GismeteoApiClient:
10397
"""Prepare Gismeteo API client instance."""
98+
if config is None:
99+
config = {}
104100
return GismeteoApiClient(
105101
async_get_clientsession(hass),
106102
latitude=config.get(CONF_LATITUDE, hass.config.latitude),
@@ -128,26 +124,12 @@ async def _async_get_coordinator(hass: HomeAssistant, unique_id, config: dict):
128124
return coordinator
129125

130126

131-
def _convert_yaml_config(config: ConfigType) -> ConfigType:
132-
"""Convert YAML config to EntryFlow config."""
133-
cfg = config.copy()
134-
135-
if CONF_SENSORS in cfg:
136-
cfg.update(cfg[CONF_SENSORS])
137-
cfg.pop(CONF_SENSORS)
138-
cfg[CONF_PLATFORM_FORMAT.format(Platform.SENSOR)] = True
139-
140-
return cfg
141-
142-
143127
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
144128
"""Set up Gismeteo as config entry."""
145129
if entry.source == SOURCE_IMPORT:
146130
# Setup from configuration.yaml
147-
for uid, cfg in hass.data[DOMAIN_YAML].items():
148-
cfg = _convert_yaml_config(cfg)
149-
150-
coordinator = await _async_get_coordinator(hass, uid, cfg)
131+
for uid, config in hass.data[DOMAIN_YAML].items():
132+
coordinator = await _async_get_coordinator(hass, uid, config)
151133
hass.data[DOMAIN][uid] = {
152134
COORDINATOR: coordinator,
153135
}

custom_components/gismeteo/config_flow.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515

1616
from homeassistant import config_entries
1717
from homeassistant.config_entries import SOURCE_IMPORT
18-
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME, Platform
18+
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
1919
from homeassistant.core import callback
2020
import homeassistant.helpers.config_validation as cv
2121

2222
from . import _get_api_client, forecast_days_int # pylint: disable=unused-import
2323
from .api import ApiError
2424
from .const import ( # pylint: disable=unused-import
25+
CONF_ADD_SENSORS,
2526
CONF_FORECAST_DAYS,
26-
CONF_PLATFORM_FORMAT,
2727
DOMAIN,
2828
)
2929

@@ -132,9 +132,7 @@ async def async_step_user(self, user_input: dict | None = None):
132132
data_schema=self.add_suggested_values_to_schema(
133133
vol.Schema(
134134
{
135-
vol.Required(
136-
CONF_PLATFORM_FORMAT.format(Platform.SENSOR)
137-
): bool,
135+
vol.Required(CONF_ADD_SENSORS, default=False): bool,
138136
vol.Optional(CONF_FORECAST_DAYS): forecast_days_int,
139137
}
140138
),

custom_components/gismeteo/const.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,13 @@
6262

6363
# Configuration and options
6464
CONF_CACHE_DIR: Final = "cache_dir"
65-
CONF_FORECAST: Final = "forecast"
65+
CONF_ADD_SENSORS: Final = "add_sensors"
6666
CONF_FORECAST_DAYS: Final = "forecast_days"
67-
CONF_PLATFORMS: Final = "platforms"
68-
CONF_YAML: Final = "_yaml"
69-
CONF_PLATFORM_FORMAT: Final = "_platform_{}"
7067

7168
# Defaults
7269
DEFAULT_NAME: Final = "Gismeteo"
7370

7471
# Attributes
75-
ATTR_LAST_UPDATED: Final = "last_updated"
76-
#
7772
ATTR_SUNRISE: Final = "sunrise"
7873
ATTR_SUNSET: Final = "sunset"
7974
#

custom_components/gismeteo/sensor.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
)
2020
from homeassistant.components.weather import ATTR_FORECAST_CONDITION
2121
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
22-
from homeassistant.const import CONF_NAME, Platform
22+
from homeassistant.const import CONF_NAME
2323
from homeassistant.core import HomeAssistant
2424
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2525
from homeassistant.helpers.typing import ConfigType, StateType
2626

27-
from . import GismeteoDataUpdateCoordinator, _convert_yaml_config, deslugify
27+
from . import GismeteoDataUpdateCoordinator, deslugify
2828
from .const import (
2929
ATTRIBUTION,
30+
CONF_ADD_SENSORS,
3031
CONF_FORECAST_DAYS,
31-
CONF_PLATFORM_FORMAT,
3232
COORDINATOR,
3333
DOMAIN,
3434
DOMAIN_YAML,
@@ -75,23 +75,23 @@ async def async_setup_entry(
7575
entities = []
7676
if config_entry.source == SOURCE_IMPORT:
7777
# Setup from configuration.yaml
78-
for uid, cfg in hass.data[DOMAIN_YAML].items():
79-
cfg = _convert_yaml_config(cfg)
80-
81-
if cfg.get(CONF_PLATFORM_FORMAT.format(Platform.SENSOR), False) is False:
78+
for uid, config in hass.data[DOMAIN_YAML].items():
79+
if config is None:
80+
config = {}
81+
if not config.get(CONF_ADD_SENSORS):
8282
continue # pragma: no cover
8383

84-
location_name = cfg.get(CONF_NAME, deslugify(uid))
84+
location_name = config.get(CONF_NAME, deslugify(uid))
8585
coordinator = hass.data[DOMAIN][uid][COORDINATOR]
8686

87-
entities.extend(_gen_entities(location_name, coordinator, cfg))
87+
entities.extend(_gen_entities(location_name, coordinator, config))
8888

8989
else:
9090
# Setup from config entry
9191
config = config_entry.data.copy() # type: ConfigType
9292
config.update(config_entry.options)
9393

94-
if config.get(CONF_PLATFORM_FORMAT.format(Platform.SENSOR), False) is False:
94+
if not config.get(CONF_ADD_SENSORS):
9595
return # pragma: no cover
9696

9797
location_name = config[CONF_NAME]

custom_components/gismeteo/translations/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@
221221
"step": {
222222
"user": {
223223
"data": {
224-
"_platform_sensor": "Sensor entities enabled",
224+
"add_sensors": "Sensor entities enabled",
225225
"forecast_days": "Forecast sensors to show (days)"
226226
},
227227
"title": "Gismeteo Options"

custom_components/gismeteo/translations/ru.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@
221221
"step": {
222222
"user": {
223223
"data": {
224-
"_platform_sensor": "Сенсоры включены",
224+
"add_sensors": "Сенсоры включены",
225225
"forecast_days": "Сенсоры прогноза на (дней)"
226226
},
227227
"title": "Настройки Gismeteo"

custom_components/gismeteo/weather.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
)
2626
from homeassistant.core import HomeAssistant, callback
2727

28-
from . import GismeteoDataUpdateCoordinator, _convert_yaml_config, deslugify
28+
from . import GismeteoDataUpdateCoordinator, deslugify
2929
from .const import ATTRIBUTION, COORDINATOR, DOMAIN, DOMAIN_YAML, ForecastMode
3030
from .entity import GismeteoEntity
3131

@@ -37,10 +37,10 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
3737
entities = []
3838
if config_entry.source == SOURCE_IMPORT:
3939
# Setup from configuration.yaml
40-
for uid, cfg in hass.data[DOMAIN_YAML].items():
41-
cfg = _convert_yaml_config(cfg)
42-
43-
location_name = cfg.get(CONF_NAME, deslugify(uid))
40+
for uid, config in hass.data[DOMAIN_YAML].items():
41+
if config is None:
42+
config = {}
43+
location_name = config.get(CONF_NAME, deslugify(uid))
4444
coordinator = hass.data[DOMAIN][uid][COORDINATOR]
4545

4646
entities.append(GismeteoWeather(coordinator, location_name))

tests/const.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
11
"""Constants for tests."""
22

3-
from custom_components.gismeteo.const import CONF_PLATFORM_FORMAT
4-
from homeassistant.const import (
5-
CONF_LATITUDE,
6-
CONF_LONGITUDE,
7-
CONF_NAME,
8-
CONF_SENSORS,
9-
Platform,
10-
)
3+
from custom_components.gismeteo.const import CONF_ADD_SENSORS
4+
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
115

12-
FAKE_NAME = "Home"
13-
FAKE_UNIQUE_ID = "test_id"
14-
FAKE_LATITUDE = 55.55
15-
FAKE_LONGITUDE = 122.12
6+
TEST_NAME = "Home"
7+
TEST_UNIQUE_ID = "test_id"
8+
TEST_LATITUDE = 55.55
9+
TEST_LONGITUDE = 122.12
1610

17-
FAKE_CONFIG = {
18-
CONF_NAME: FAKE_NAME,
19-
CONF_LATITUDE: FAKE_LATITUDE,
20-
CONF_LONGITUDE: FAKE_LONGITUDE,
11+
TEST_CONFIG = {
12+
CONF_NAME: TEST_NAME,
13+
CONF_LATITUDE: TEST_LATITUDE,
14+
CONF_LONGITUDE: TEST_LONGITUDE,
2115
}
2216

23-
FAKE_CONFIG_OPTIONS = {
24-
CONF_PLATFORM_FORMAT.format(Platform.SENSOR): True,
17+
TEST_CONFIG_OPTIONS = {
18+
CONF_ADD_SENSORS: True,
2519
}
2620

27-
FAKE_CONFIG_YAML = {
21+
TEST_CONFIG_YAML = {
2822
"home": {
29-
CONF_NAME: FAKE_NAME,
30-
CONF_LATITUDE: FAKE_LATITUDE,
31-
CONF_LONGITUDE: FAKE_LONGITUDE,
32-
CONF_SENSORS: {},
23+
CONF_NAME: TEST_NAME,
24+
CONF_LATITUDE: TEST_LATITUDE,
25+
CONF_LONGITUDE: TEST_LONGITUDE,
26+
CONF_ADD_SENSORS: True,
3327
},
3428
}

0 commit comments

Comments
 (0)