-
-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
98 additions
and
16 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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
from .device import Device, DeviceInfo | ||
from .hass_entry import HassEntry | ||
from .hass_entity import XEntity | ||
|
||
__all__ = [ | ||
'Device', | ||
'DeviceInfo', | ||
'HassEntry', | ||
'XEntity', | ||
] |
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,63 @@ | ||
import logging | ||
from typing import TYPE_CHECKING, Callable | ||
|
||
from homeassistant.helpers.entity import Entity, DeviceInfo | ||
|
||
from .miot_spec import MiotService, MiotProperty | ||
|
||
if TYPE_CHECKING: | ||
from .device import Device | ||
from .converters import BaseConv | ||
|
||
_LOGGER = logging.getLogger(__package__) | ||
|
||
class XEntity(Entity): | ||
CLS: dict[str, Callable] = {} | ||
|
||
log = _LOGGER | ||
added = False | ||
_attr_should_poll = False | ||
_attr_has_entity_name = True | ||
|
||
def __init__(self, device: 'Device', conv: 'BaseConv'): | ||
self.device = device | ||
self.hass = device.hass | ||
self.conv = conv | ||
attr = conv.attr | ||
if isinstance(attr, MiotProperty): | ||
self.attr = attr.full_name | ||
self._attr_translation_key = attr.friendly_name | ||
self.entity_id = attr.generate_entity_id(self, conv.domain) | ||
elif isinstance(attr, MiotService): | ||
self.attr = attr.name | ||
self._attr_translation_key = attr.name | ||
self.entity_id = attr.generate_entity_id(self, conv.domain) | ||
else: | ||
self.attr = attr | ||
self._attr_translation_key = attr | ||
|
||
self.on_init() | ||
|
||
@property | ||
def unique_mac(self): | ||
return self.device.info.unique_id | ||
|
||
def on_init(self): | ||
"""Run on class init.""" | ||
|
||
def on_device_update(self, data: dict): | ||
pass | ||
|
||
def get_state(self) -> dict: | ||
"""Run before entity remove if entity is subclass from RestoreEntity.""" | ||
return {} | ||
|
||
def set_state(self, data: dict): | ||
"""Run on data from device.""" | ||
self._attr_state = data.get(self.attr) | ||
|
||
async def async_added_to_hass(self) -> None: | ||
self.device.add_listener(self.on_device_update) | ||
|
||
async def async_will_remove_from_hass(self) -> None: | ||
self.device.remove_listener(self.on_device_update) |
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