Skip to content

Commit

Permalink
Move velux base entity to separate module (#126185)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Sep 18, 2024
1 parent da4f401 commit 93de46b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 36 deletions.
35 changes: 2 additions & 33 deletions homeassistant/components/velux/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""Support for VELUX KLF 200 devices."""

from pyvlx import Node, PyVLX, PyVLXException
from pyvlx import PyVLX, PyVLXException

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PASSWORD, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.helpers.entity import Entity
from homeassistant.core import HomeAssistant, ServiceCall

from .const import DOMAIN, LOGGER, PLATFORMS

Expand Down Expand Up @@ -67,33 +66,3 @@ async def async_start(self):
LOGGER.debug("Velux interface started")
await self.pyvlx.load_scenes()
await self.pyvlx.load_nodes()


class VeluxEntity(Entity):
"""Abstraction for al Velux entities."""

_attr_should_poll = False

def __init__(self, node: Node, config_entry_id: str) -> None:
"""Initialize the Velux device."""
self.node = node
self._attr_unique_id = (
node.serial_number
if node.serial_number
else f"{config_entry_id}_{node.node_id}"
)
self._attr_name = node.name if node.name else f"#{node.node_id}"

@callback
def async_register_callbacks(self):
"""Register callbacks to update hass after device was changed."""

async def after_update_callback(device):
"""Call after device was updated."""
self.async_write_ha_state()

self.node.register_device_updated_cb(after_update_callback)

async def async_added_to_hass(self):
"""Store register state change callback."""
self.async_register_callbacks()
3 changes: 2 additions & 1 deletion homeassistant/components/velux/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import DOMAIN, VeluxEntity
from .const import DOMAIN
from .entity import VeluxEntity

PARALLEL_UPDATES = 1

Expand Down
36 changes: 36 additions & 0 deletions homeassistant/components/velux/entity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Support for VELUX KLF 200 devices."""

from pyvlx import Node

from homeassistant.core import callback
from homeassistant.helpers.entity import Entity


class VeluxEntity(Entity):
"""Abstraction for al Velux entities."""

_attr_should_poll = False

def __init__(self, node: Node, config_entry_id: str) -> None:
"""Initialize the Velux device."""
self.node = node
self._attr_unique_id = (
node.serial_number
if node.serial_number
else f"{config_entry_id}_{node.node_id}"
)
self._attr_name = node.name if node.name else f"#{node.node_id}"

@callback
def async_register_callbacks(self):
"""Register callbacks to update hass after device was changed."""

async def after_update_callback(device):
"""Call after device was updated."""
self.async_write_ha_state()

self.node.register_device_updated_cb(after_update_callback)

async def async_added_to_hass(self):
"""Store register state change callback."""
self.async_register_callbacks()
3 changes: 2 additions & 1 deletion homeassistant/components/velux/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import DOMAIN, VeluxEntity
from .const import DOMAIN
from .entity import VeluxEntity

PARALLEL_UPDATES = 1

Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/velux/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import DOMAIN
from .const import DOMAIN

PARALLEL_UPDATES = 1

Expand Down

0 comments on commit 93de46b

Please sign in to comment.