-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move configuration to config_flow (#18)
- Loading branch information
Showing
14 changed files
with
531 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
import logging | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
|
||
from mobilus_client.app import App as MobilusClientApp | ||
from mobilus_client.config import Config as MobilusClientConfig | ||
|
||
from .const import DOMAIN, PLATFORMS, SUPPORTED_DEVICES | ||
from .coordinator import MobilusCoordinator | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
hass.data.setdefault(DOMAIN, {}) | ||
|
||
client_config = MobilusClientConfig( | ||
gateway_host=entry.data["host"], | ||
user_login=entry.data["username"], | ||
user_password=entry.data["password"], | ||
) | ||
client = MobilusClientApp(client_config) | ||
|
||
# Retrieve devices list | ||
response = await hass.async_add_executor_job( | ||
lambda: json.loads(client.call([("devices_list", {})])), | ||
) | ||
|
||
if not response: | ||
_LOGGER.warning("No devices found in response.") | ||
return False | ||
|
||
devices = response[0].get("devices", []) | ||
|
||
if not devices: | ||
_LOGGER.warning("No devices found in the devices list.") | ||
return False | ||
|
||
# Currently non cover devices are not supported | ||
supported_devices = [ | ||
device for device in devices | ||
if device["type"] in SUPPORTED_DEVICES | ||
] | ||
|
||
if not supported_devices: | ||
_LOGGER.warning("No supported devices found in the devices list.") | ||
return False | ||
|
||
coordinator = MobilusCoordinator(hass, client) | ||
|
||
hass.data[DOMAIN][entry.entry_id] = { | ||
"client": client, | ||
"coordinator": coordinator, | ||
"devices": supported_devices, | ||
} | ||
|
||
await coordinator.async_config_entry_first_refresh() | ||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) | ||
|
||
return True | ||
|
||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) | ||
|
||
if unload_ok: | ||
hass.data[DOMAIN].pop(entry.entry_id) | ||
|
||
return unload_ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
import voluptuous as vol | ||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult | ||
|
||
from .const import DOMAIN | ||
|
||
|
||
class MobilusConfigFlow(ConfigFlow, domain=DOMAIN): | ||
VERSION = 1 | ||
|
||
async def async_step_user(self, user_input: dict[str, Any] | None = None) -> ConfigFlowResult: | ||
if user_input is not None: | ||
return self.async_create_entry( | ||
title=user_input["host"], | ||
data=user_input, | ||
) | ||
|
||
return self.async_show_form( | ||
step_id="user", | ||
data_schema=self._data_schema(), | ||
) | ||
|
||
async def async_step_reconfigure(self, user_input: dict[str, Any] | None = None) -> ConfigFlowResult: | ||
reconfigure_entry = self.hass.config_entries.async_get_entry( | ||
self.context["entry_id"], | ||
) | ||
|
||
if reconfigure_entry is None: | ||
return self.async_abort(reason="entry_not_found") | ||
|
||
if user_input is not None: | ||
return self.async_update_reload_and_abort( | ||
entry=reconfigure_entry, | ||
data=user_input, | ||
reason="reconfigure_successful", | ||
) | ||
|
||
return self.async_show_form( | ||
step_id="reconfigure", | ||
data_schema=self._data_schema(dict(reconfigure_entry.data)), | ||
) | ||
|
||
def _data_schema(self, defaults: dict[str, Any] | None = None) -> vol.Schema: | ||
defaults = defaults or {} | ||
|
||
return vol.Schema({ | ||
vol.Required("host", default=defaults.get("host", None)): str, | ||
vol.Required("username", default=defaults.get("username", None)): str, | ||
vol.Required("password", default=defaults.get("password", None)): str, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"config": { | ||
"step": { | ||
"user": { | ||
"title": "Connect to Mobilus COSMO GTW", | ||
"description": "Please enter your Mobilus COSMO GTW host and credentials.", | ||
"data": { | ||
"host": "IP Address / Host", | ||
"username": "Username", | ||
"password": "Password" | ||
} | ||
}, | ||
"reconfigure": { | ||
"title": "Reconfigure Mobilus COSMO GTW", | ||
"description": "Please enter your Mobilus COSMO GTW host and credentials.", | ||
"data": { | ||
"host": "IP Address / Host", | ||
"username": "Username", | ||
"password": "Password" | ||
} | ||
} | ||
}, | ||
"error": { | ||
"entry_not_found": "Configuration entry not found.", | ||
"reconfigure_successful": "Reconfiguration has been saved. If the data is incorrect, please enter the correct data again." | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"config": { | ||
"step": { | ||
"user": { | ||
"title": "Connect to Mobilus COSMO GTW", | ||
"description": "Please enter your Mobilus COSMO GTW host and credentials.", | ||
"data": { | ||
"host": "IP Address / Host", | ||
"username": "Username", | ||
"password": "Password" | ||
} | ||
}, | ||
"reconfigure": { | ||
"title": "Reconfigure Mobilus COSMO GTW", | ||
"description": "Please enter your Mobilus COSMO GTW host and credentials.", | ||
"data": { | ||
"host": "IP Address / Host", | ||
"username": "Username", | ||
"password": "Password" | ||
} | ||
} | ||
}, | ||
"error": { | ||
"entry_not_found": "Configuration entry not found.", | ||
"reconfigure_successful": "Reconfiguration has been saved. If the data is incorrect, please enter the correct data again." | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"config": { | ||
"step": { | ||
"user": { | ||
"title": "Podaj dane połączenia", | ||
"description": "Proszę podać dane do połączenia z bramką Mobilus COSMO GTW.", | ||
"data": { | ||
"host": "Adres IP / Host", | ||
"username": "Nazwa użytkownika", | ||
"password": "Hasło" | ||
} | ||
}, | ||
"reconfigure": { | ||
"title": "Zmień dane połączenia", | ||
"description": "Proszę podać dane do połączenia z bramką Mobilus COSMO GTW.", | ||
"data": { | ||
"host": "Adres IP / Host", | ||
"username": "Nazwa użytkownika", | ||
"password": "Hasło" | ||
} | ||
} | ||
}, | ||
"error": { | ||
"entry_not_found": "Konfiguracja nie została znaleziona.", | ||
"reconfigure_successful": "Ponowna konfiguracja została zapisana. W przypadku błędnych danych, proszę ponownie wprowadzić poprawne dane." | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"name": "Mobilus Cosmo GTW", | ||
"homeassistant": "2022.5.0" | ||
"homeassistant": "2024.4.0" | ||
} |
Oops, something went wrong.