-
-
Notifications
You must be signed in to change notification settings - Fork 37.1k
Refactor Velux integration #42773
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
Closed
Closed
Refactor Velux integration #42773
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8ace058
Velux refactor
pawlizio 00247cf
several updates
pawlizio 7322c49
Update en.json
pawlizio b54b05e
Update strings.json
pawlizio 9713708
Update en.json
pawlizio a81e9fe
Update config_flow.py
pawlizio 242bab2
Update const.py
pawlizio ae3a453
Update scene.py
pawlizio 9b9b671
Update strings.json
pawlizio b5c19dc
Update en.json
pawlizio 52e7286
Update en.json
pawlizio 61a6b9b
Add ConfigEntryNotReady exception
pawlizio c9ea076
Update __init__.py
pawlizio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,70 +1,66 @@ | ||
| """Support for VELUX KLF 200 devices.""" | ||
| import logging | ||
|
|
||
| from pyvlx import PyVLX, PyVLXException | ||
| import voluptuous as vol | ||
| from pyvlx import PyVLX | ||
|
|
||
| from homeassistant.const import CONF_HOST, CONF_PASSWORD, EVENT_HOMEASSISTANT_STOP | ||
| from homeassistant.helpers import discovery | ||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import CONF_HOST, CONF_PASSWORD | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.exceptions import ConfigEntryNotReady | ||
|
|
||
| from .const import DOMAIN, PLATFORMS | ||
|
|
||
| DOMAIN = "velux" | ||
| DATA_VELUX = "data_velux" | ||
| SUPPORTED_DOMAINS = ["cover", "scene"] | ||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema( | ||
| { | ||
| DOMAIN: vol.Schema( | ||
| {vol.Required(CONF_HOST): cv.string, vol.Required(CONF_PASSWORD): cv.string} | ||
|
|
||
| async def async_setup(hass, config): | ||
| """Set up the Velux KLF platform via configuration.yaml.""" | ||
| if DOMAIN in config: | ||
| hass.async_create_task( | ||
| hass.config_entries.flow.async_init( | ||
| DOMAIN, context={"source": "import"}, data=config[DOMAIN] | ||
| ) | ||
| ) | ||
| }, | ||
| extra=vol.ALLOW_EXTRA, | ||
| ) | ||
| return True | ||
|
|
||
|
|
||
| async def async_setup(hass, config): | ||
| """Set up the velux component.""" | ||
| try: | ||
| hass.data[DATA_VELUX] = VeluxModule(hass, config[DOMAIN]) | ||
| hass.data[DATA_VELUX].setup() | ||
| await hass.data[DATA_VELUX].async_start() | ||
| async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): | ||
| """Set up the Velux KLF platforms via Config Flow.""" | ||
| _LOGGER.debug("Setting up velux entry: %s", entry.data) | ||
| host = entry.data[CONF_HOST] | ||
| password = entry.data[CONF_PASSWORD] | ||
| gateway = PyVLX(host=host, password=password) | ||
|
|
||
| except PyVLXException as ex: | ||
| _LOGGER.exception("Can't connect to velux interface: %s", ex) | ||
| return False | ||
| hass.data.setdefault(DOMAIN, {}) | ||
| hass.data[DOMAIN][entry.entry_id] = gateway | ||
| try: | ||
| await gateway.connect() | ||
| except (OSError, ConnectionAbortedError) as ex: | ||
| _LOGGER.error("Unable to connect to KLF200: %s", str(ex)) | ||
| raise ConfigEntryNotReady from ex | ||
| await gateway.load_nodes() | ||
| await gateway.load_scenes() | ||
|
|
||
| for component in SUPPORTED_DOMAINS: | ||
| for component in PLATFORMS: | ||
| hass.async_create_task( | ||
| discovery.async_load_platform(hass, component, DOMAIN, {}, config) | ||
| hass.config_entries.async_forward_entry_setup(entry, component) | ||
| ) | ||
| return True | ||
|
|
||
| async def async_reboot_gateway(service_call): | ||
| await gateway.reboot_gateway() | ||
|
|
||
| class VeluxModule: | ||
| """Abstraction for velux component.""" | ||
| hass.services.async_register(DOMAIN, "reboot_gateway", async_reboot_gateway) | ||
|
|
||
| def __init__(self, hass, domain_config): | ||
| """Initialize for velux component.""" | ||
| self.pyvlx = None | ||
| self._hass = hass | ||
| self._domain_config = domain_config | ||
| return True | ||
|
|
||
| def setup(self): | ||
| """Velux component setup.""" | ||
|
|
||
| async def on_hass_stop(event): | ||
| """Close connection when hass stops.""" | ||
| _LOGGER.debug("Velux interface terminated") | ||
| await self.pyvlx.disconnect() | ||
| async def async_unload_entry(hass, entry): | ||
| """Unloading the Velux platform.""" | ||
| gateway = hass.data[DOMAIN][entry.entry_id] | ||
| await gateway.reboot_gateway() | ||
| await gateway.disconnect() | ||
|
|
||
| self._hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop) | ||
| host = self._domain_config.get(CONF_HOST) | ||
| password = self._domain_config.get(CONF_PASSWORD) | ||
| self.pyvlx = PyVLX(host=host, password=password) | ||
| for component in PLATFORMS: | ||
| await hass.config_entries.async_forward_entry_unload(entry, component) | ||
|
|
||
| async def async_start(self): | ||
| """Start velux component.""" | ||
| _LOGGER.debug("Velux interface started") | ||
| await self.pyvlx.load_scenes() | ||
| await self.pyvlx.load_nodes() | ||
| return True |
This file contains hidden or 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,91 @@ | ||
| """Velux component config flow.""" | ||
| # https://developers.home-assistant.io/docs/config_entries_config_flow_handler#defining-your-config-flow | ||
| import logging | ||
|
|
||
| from pyvlx import PyVLX, PyVLXException | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant import config_entries | ||
| from homeassistant.const import CONF_HOST, CONF_PASSWORD | ||
|
|
||
| from .const import DOMAIN | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| RESULT_AUTH_FAILED = "connection_failed" | ||
| RESULT_SUCCESS = "success" | ||
|
|
||
|
|
||
| class VeluxConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): | ||
| """Velux config flow.""" | ||
|
|
||
| def __init__(self): | ||
| """Initialize.""" | ||
| self._velux = None | ||
| self._host = None | ||
| self._password = None | ||
| self._hostname = None | ||
| self.bridge = None | ||
|
|
||
| def _get_entry(self): | ||
| return self.async_create_entry( | ||
| title=self._host, | ||
| data={CONF_HOST: self._host, CONF_PASSWORD: self._password}, | ||
| ) | ||
|
|
||
| async def async_step_import(self, user_input=None): | ||
| """Handle configuration by yaml file.""" | ||
| return await self.async_step_user(user_input) | ||
|
|
||
| async def async_step_user(self, user_input=None): | ||
| """Handle configuration via user input.""" | ||
| errors = {} | ||
| if user_input is not None: | ||
| self._host = user_input[CONF_HOST] | ||
| self._password = user_input[CONF_PASSWORD] | ||
| await self.async_set_unique_id(self._host) | ||
| self._abort_if_unique_id_configured() | ||
| self.bridge = PyVLX(host=self._host, password=self._password) | ||
| try: | ||
| await self.bridge.connect() | ||
| await self.bridge.disconnect() | ||
| return self._get_entry() | ||
| except PyVLXException: | ||
| errors["base"] = "invalid_auth" | ||
| except OSError: | ||
| errors["base"] = "invalid_host" | ||
| else: | ||
| errors["base"] = "cannot_connect" | ||
|
|
||
| data_schema = vol.Schema( | ||
| { | ||
| vol.Required(CONF_HOST, default=self._host): str, | ||
| vol.Required(CONF_PASSWORD, default=self._password): str, | ||
| } | ||
| ) | ||
|
|
||
| return self.async_show_form( | ||
| step_id="user", data_schema=data_schema, errors=errors | ||
| ) | ||
|
|
||
| async def async_step_unignore(self, user_input): | ||
| """Rediscover a previously ignored discover.""" | ||
| unique_id = user_input["unique_id"] | ||
| await self.async_set_unique_id(unique_id) | ||
| return await self.async_step_user() | ||
|
|
||
| async def async_step_zeroconf(self, info): | ||
| """Handle discovery by zeroconf.""" | ||
| if ( | ||
| info is None | ||
| or not info.get("hostname") | ||
| or not info["hostname"].startswith("VELUX_KLF_LAN") | ||
| ): | ||
| return self.async_abort(reason="no_devices_found") | ||
|
|
||
| self._host = info.get("host") | ||
|
|
||
| await self.async_set_unique_id(self._host) | ||
| self._abort_if_unique_id_configured() | ||
|
|
||
| return await self.async_step_user() | ||
This file contains hidden or 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,4 @@ | ||
| """Constants for Velux Integration.""" | ||
|
|
||
| DOMAIN = "velux" | ||
| PLATFORMS = ["cover", "scene"] |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -2,6 +2,14 @@ | |
| "domain": "velux", | ||
| "name": "Velux", | ||
| "documentation": "https://www.home-assistant.io/integrations/velux", | ||
| "requirements": ["pyvlx==0.2.16"], | ||
| "codeowners": ["@Julius2342"] | ||
| } | ||
| "codeowners": [ | ||
| "@Julius2342" | ||
| ], | ||
| "requirements": [ | ||
| "pyvlx==0.2.18" | ||
| ], | ||
| "zeroconf": [ | ||
| "_http._tcp.local." | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update this to include a name/Mac filter: https://developers.home-assistant.io/docs/creating_integration_manifest#zeroconf |
||
| ], | ||
| "config_flow": true | ||
| } | ||
This file contains hidden or 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 hidden or 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,4 @@ | ||
| # Velux Integration services | ||
|
|
||
| reboot_gateway: | ||
| description: Reboots the KLF200 Gateway. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to add this step.