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

Add config flow, service info & translations #96

Merged
merged 59 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
12b6dd5
Config flow initial implementation
pnbruckner Nov 7, 2023
e08a0c1
black
pnbruckner Nov 7, 2023
9139fed
Incremental checkin
pnbruckner Nov 8, 2023
134ee7e
Move settings from config data to options
pnbruckner Nov 9, 2023
7552a4b
Add service via _attr_device_info
pnbruckner Nov 9, 2023
c0fa25b
Sort manifest.json
pnbruckner Nov 10, 2023
87c7088
Fix 2023.3 compatibility issue
pnbruckner Nov 10, 2023
c6e5a75
Use has_entity_name, sun2 config name is now location
pnbruckner Nov 11, 2023
75e1d1d
Update config entries per configuration
pnbruckner Nov 11, 2023
eb8fc15
First translation implementation
pnbruckner Nov 11, 2023
2fc9495
Add remaining translations
pnbruckner Nov 11, 2023
ab1d6d1
Simplify config entry updating
pnbruckner Nov 12, 2023
5df731e
Remove simple entity types from sun2 config
pnbruckner Nov 12, 2023
5fe55ed
Add nl.json. Thanks metbril!
pnbruckner Nov 12, 2023
ce13b92
Update info.md
pnbruckner Nov 12, 2023
eac6d41
Update README.md
pnbruckner Nov 12, 2023
9bedd11
Bump version to 3.0.0b0 & point doc to branch
pnbruckner Nov 13, 2023
df1c623
Fix config bug when no sensors listed
pnbruckner Nov 13, 2023
d6ae933
Bump version to 3.0.0b1
pnbruckner Nov 13, 2023
28a6b0c
Fix core config update handling
pnbruckner Nov 13, 2023
643bf3e
Bump version to 3.0.0b2
pnbruckner Nov 13, 2023
927c165
Move processing of HA location config to __init__
pnbruckner Nov 14, 2023
6634116
Fix time_at_elevation when config removed from YAML
pnbruckner Nov 14, 2023
b94f47c
Bump version to 3.0.0b3
pnbruckner Nov 14, 2023
19a355c
Translations require HA 2023.4.0 or newer
pnbruckner Nov 14, 2023
9a3d5b3
Move translation lookup to __init__
pnbruckner Nov 15, 2023
f36bb6d
Move config validation to config.py
pnbruckner Nov 15, 2023
ed7cdef
More translations of default entity names
pnbruckner Nov 15, 2023
28c256e
Bump version to 3.0.0b4
pnbruckner Nov 15, 2023
16c9c29
More translations, including attribute names & states
pnbruckner Nov 16, 2023
e497d26
Add PACKAGE_MERGE_HINT
pnbruckner Nov 16, 2023
e3fa933
Add _unreported_attributes
pnbruckner Nov 16, 2023
29e3c63
Make elevation binary sensor config similar to others
pnbruckner Nov 16, 2023
5234667
Add required unique_id for non-simple entity configs
pnbruckner Nov 17, 2023
d176d55
Better handle core config update
pnbruckner Nov 17, 2023
78fa138
Update README.md
pnbruckner Nov 17, 2023
de8e247
Bump version to 3.0.0b5
pnbruckner Nov 17, 2023
5cab09a
Add reload service
pnbruckner Nov 17, 2023
3fcd761
Update README.md
pnbruckner Nov 18, 2023
1628439
Bump version to 3.0.0b6
pnbruckner Nov 18, 2023
82572d2
Use placeholders in misc translations
pnbruckner Nov 18, 2023
7143890
Bump version to 3.0.0b7
pnbruckner Nov 18, 2023
75d6c5d
Add sun2 integration via UI
pnbruckner Nov 21, 2023
34b775f
Add non-simple sensors via UI
pnbruckner Nov 28, 2023
87aa5f7
Update info.md
pnbruckner Nov 28, 2023
fb9bd2c
Update README.md
pnbruckner Nov 29, 2023
94210d7
Move misc translations to satisfy hassfest
pnbruckner Nov 29, 2023
6dce3d6
Update names of additional entities when language is changed
pnbruckner Dec 1, 2023
1c21156
Update README.md
pnbruckner Dec 1, 2023
4c590f7
Bump version to 3.0.0b9
pnbruckner Dec 1, 2023
3902be5
Miscellaneous improvements
pnbruckner Dec 4, 2023
30ee800
ruff, pylint, black & mypy
pnbruckner Dec 5, 2023
4341508
Fix bug with use of abstractmethod & property decorators.
pnbruckner Dec 6, 2023
2980839
Fix bug in hours_to_hms introduced in previous commit
pnbruckner Dec 7, 2023
5a4323f
Use only new style selectors
pnbruckner Dec 8, 2023
ff14d49
Simplify unique IDs for UI added additional sensors
pnbruckner Dec 8, 2023
a30b1be
Add option to remove additional entities
pnbruckner Dec 9, 2023
11ab5c2
Bump version to 3.0.0b10
pnbruckner Dec 9, 2023
b0cd85b
Fix _unrecorded_attributes
pnbruckner Dec 11, 2023
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
Prev Previous commit
Next Next commit
Move translation lookup to __init__
  • Loading branch information
pnbruckner committed Nov 15, 2023
commit 9a3d5b30170a3afe09f324a8d24cd4ce1cb6c371
14 changes: 11 additions & 3 deletions custom_components/sun2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Sun2 integration."""
from __future__ import annotations

from typing import cast

import voluptuous as vol

from homeassistant.config_entries import ConfigEntry, SOURCE_IMPORT
Expand All @@ -15,11 +17,12 @@
from homeassistant.core import Event, HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import dispatcher_send
from homeassistant.helpers.translation import async_get_translations
from homeassistant.helpers.typing import ConfigType

from .binary_sensor import SUN2_BINARY_SENSOR_SCHEMA
from .const import DOMAIN, SIG_HA_LOC_UPDATED
from .helpers import LOC_PARAMS, LocData, LocParams
from .helpers import LOC_PARAMS, LocData, LocParams, Sun2Data
from .sensor import ELEVATION_AT_TIME_SCHEMA, TIME_AT_ELEVATION_SCHEMA


Expand Down Expand Up @@ -60,11 +63,16 @@ def _unique_locations_names(configs: list[dict]) -> list[dict]:

async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Setup composite integration."""
hass.data[DOMAIN] = {}
hass.data[DOMAIN] = Sun2Data(
locations={},
translations=await async_get_translations(
hass, hass.config.language, "misc", [DOMAIN], False
),
)

def update_local_loc_data(event: Event | None = None) -> None:
"""Update local location data from HA's config."""
hass.data[DOMAIN][None] = loc_data = LocData(
cast(Sun2Data, hass.data[DOMAIN]).locations[None] = loc_data = LocData(
LocParams(
hass.config.elevation,
hass.config.latitude,
Expand Down
12 changes: 6 additions & 6 deletions custom_components/sun2/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""Config flow for Sun2 integration."""
from __future__ import annotations

from typing import Any
from typing import cast, Any

from homeassistant.config_entries import ConfigFlow
from homeassistant.const import CONF_LOCATION, CONF_UNIQUE_ID
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.translation import async_get_translations

from .const import DOMAIN
from .helpers import Sun2Data


class Sun2ConfigFlow(ConfigFlow, domain=DOMAIN):
Expand All @@ -18,11 +18,11 @@ class Sun2ConfigFlow(ConfigFlow, domain=DOMAIN):

async def async_step_import(self, data: dict[str, Any]) -> FlowResult:
"""Import config entry from configuration."""
translations = await async_get_translations(
self.hass, self.hass.config.language, "service_name", [DOMAIN], False
)
location = data.pop(CONF_LOCATION, self.hass.config.location_name)
title = f"{location} {translations[f'component.{DOMAIN}.service_name']}"
service_name = cast(Sun2Data, self.hass.data[DOMAIN]).translations[
f"component.{DOMAIN}.misc.service_name"
]
title = f"{location} {service_name}"
if existing_entry := await self.async_set_unique_id(data.pop(CONF_UNIQUE_ID)):
self.hass.config_entries.async_update_entry(
existing_entry, title=title, options=data
Expand Down
16 changes: 14 additions & 2 deletions custom_components/sun2/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ def __init__(self, lp: LocParams) -> None:
object.__setattr__(self, "tzi", dt_util.get_time_zone(lp.time_zone))


@dataclass
class Sun2Data:
"""Sun2 shared data."""

locations: dict[LocParams | None, LocData]
translations: dict[str, str]


def get_loc_params(config: ConfigType) -> LocParams | None:
"""Get location parameters from configuration."""
try:
Expand Down Expand Up @@ -140,6 +148,10 @@ def __init__(
self._loc_params = loc_params
self.async_on_remove(self._cancel_update)

@property
def _sun2_data(self) -> Sun2Data:
return cast(Sun2Data, self.hass.data[DOMAIN])

async def async_update(self) -> None:
"""Update state."""
if not self._loc_data:
Expand All @@ -162,9 +174,9 @@ def _get_loc_data(self) -> LocData:
loc_params = None -> Use location parameters from HA's config.
"""
try:
loc_data = cast(LocData, self.hass.data[DOMAIN][self._loc_params])
loc_data = self._sun2_data.locations[self._loc_params]
except KeyError:
loc_data = self.hass.data[DOMAIN][self._loc_params] = LocData(
loc_data = self._sun2_data.locations[self._loc_params] = LocData(
cast(LocParams, self._loc_params)
)

Expand Down
4 changes: 3 additions & 1 deletion custom_components/sun2/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"title": "Sun2",
"service_name": "Sun",
"misc": {
"service_name": "Sun"
},
"entity": {
"sensor": {
"astronomical_daylight": {
Expand Down
4 changes: 3 additions & 1 deletion custom_components/sun2/translations/nl.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"title": "Zon2",
"service_name": "Zon",
"misc": {
"service_name": "Zon"
},
"entity": {
"sensor": {
"astronomical_daylight": {
Expand Down
Loading