-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Entities and Devices Appear Now. Also added support for
1. Motion Sensor 2. Open Close Sensor
- Loading branch information
Showing
11 changed files
with
429 additions
and
59 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,115 @@ | ||
from homeassistant import config_entries, core | ||
from homeassistant.const import CONF_IP_ADDRESS, CONF_TOKEN | ||
from homeassistant.core import HomeAssistantError | ||
from homeassistant.helpers.entity import DeviceInfo | ||
from homeassistant.components.binary_sensor import BinarySensorEntity | ||
|
||
import dirigera | ||
from .dirigera_lib_patch import HubX | ||
|
||
from .const import DOMAIN | ||
from .mocks.ikea_motion_sensor_mock import ikea_motion_sensor_mock | ||
from .mocks.ikea_open_close_mock import ikea_open_close_mock | ||
|
||
import logging | ||
logger = logging.getLogger("custom_components.dirigera_platform") | ||
|
||
async def async_setup_entry( | ||
hass: core.HomeAssistant, | ||
config_entry: config_entries.ConfigEntry, | ||
async_add_entities, | ||
): | ||
logger.debug("Binary Sensor Starting async_setup_entry") | ||
"""Setup sensors from a config entry created in the integrations UI.""" | ||
config = hass.data[DOMAIN][config_entry.entry_id] | ||
logger.debug(config) | ||
|
||
#hub = dirigera.Hub(config[CONF_TOKEN], config[CONF_IP_ADDRESS]) | ||
hub = HubX(config[CONF_TOKEN], config[CONF_IP_ADDRESS]) | ||
|
||
lights = [] | ||
|
||
# If mock then start with mocks | ||
if config[CONF_IP_ADDRESS] == "mock": | ||
logger.warning("Setting up mock motion sensors") | ||
mock_motion_sensor1 = ikea_motion_sensor_mock(hub,"mock_motion_sensor1") | ||
mock_motion_sensor2 = ikea_motion_sensor_mock(hub,"mock_motion_sensor2") | ||
motion_sensors = [mock_motion_sensor1,mock_motion_sensor2] | ||
|
||
logger.warning("Setting up mock open/close sensors") | ||
mock_open_close_sensor1 = ikea_open_close_mock(hub,"mock_open_close_sensor1") | ||
mock_open_close_sensor2 = ikea_open_close_mock(hub,"mock_open_close_sensor2") | ||
open_close_sensors = [mock_open_close_sensor1,mock_open_close_sensor2] | ||
|
||
else: | ||
hub_motion_sensors = await hass.async_add_executor_job(hub.get_motion_sensors) | ||
motion_sensors = [ikea_motion_sensor(hub, motion_sensor) for motion_sensor in hub_motion_sensors] | ||
|
||
hub_open_close_sensors = await hass.async_add_executor_job(hub.get_open_close_sensors) | ||
open_close_sensors = [ikea_open_close(hub, open_close_sensor) for open_close_sensor in hub_open_close_sensors] | ||
|
||
logger.debug("Found {} motion_sensor entities to setup...".format(len(motion_sensors))) | ||
async_add_entities(motion_sensors) | ||
logger.debug("Found {} open close entities to setup...".format(len(open_close_sensors))) | ||
async_add_entities(open_close_sensors) | ||
|
||
logger.debug("Binary Sensor Complete async_setup_entry") | ||
|
||
class ikea_motion_sensor(BinarySensorEntity): | ||
def __init__(self, hub, json_data): | ||
logger.debug("ikea_motion_sensor ctor...") | ||
self._hub = hub | ||
self._json_data = json_data | ||
|
||
@property | ||
def unique_id(self): | ||
return self._json_data.id | ||
|
||
@property | ||
def available(self): | ||
return self._json_data.is_reachable | ||
|
||
@property | ||
def device_info(self) -> DeviceInfo: | ||
return DeviceInfo( | ||
identifiers={("dirigera_platform",self._json_data.id)}, | ||
name = self._json_data.attributes.custom_name, | ||
manufacturer = self._json_data.attributes.manufacturer, | ||
model=self._json_data.attributes.model , | ||
sw_version=self._json_data.attributes.firmware_version | ||
) | ||
|
||
@property | ||
def name(self): | ||
return self._json_data.attributes.custom_name | ||
|
||
@property | ||
def is_on(self): | ||
return self._json_data.attributes.is_on | ||
|
||
def update(self): | ||
logger.debug("motion sensor update...") | ||
try: | ||
self._json_data = self._hub.get_motion_sensor_by_id(self._json_data.id) | ||
except Exception as ex: | ||
logger.error("error encountered running update on : {}".format(self.name)) | ||
logger.error(ex) | ||
raise HomeAssistantError(ex,DOMAIN,"hub_exception") | ||
|
||
class ikea_open_close(ikea_motion_sensor): | ||
def __init__(self, hub, json_data): | ||
logger.debug("ikea_open_close ctor...") | ||
self._hub = hub | ||
self._json_data = json_data | ||
|
||
def update(self): | ||
logger.debug("open close sensor update...") | ||
try: | ||
self._json_data = self._hub.get_open_close_by_id(self._json_data.id) | ||
except Exception as ex: | ||
logger.error("error encountered running update on : {}".format(self.name)) | ||
logger.error(ex) | ||
raise HomeAssistantError(ex,DOMAIN,"hub_exception") | ||
|
||
def is_on(self): | ||
return self._json_data.attributes.is_open |
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,40 @@ | ||
from dirigera import Hub | ||
from dirigera.devices.device import Attributes | ||
from dirigera.devices.motion_sensor import MotionSensor | ||
from dirigera.devices.open_close_sensor import OpenCloseSensor, dict_to_open_close_sensor | ||
from dirigera.hub.abstract_smart_home_hub import AbstractSmartHomeHub | ||
from typing import Any, Dict, List | ||
|
||
# Patch to fix issues with motion sensor | ||
|
||
class MotionSensorAttributesX(Attributes): | ||
battery_percentage: int | ||
is_on: bool | ||
|
||
class MotionSensorX(MotionSensor): | ||
dirigera_client: AbstractSmartHomeHub | ||
attributes: MotionSensorAttributesX | ||
|
||
def dict_to_motion_sensorx(data: Dict[str, Any], dirigera_client: AbstractSmartHomeHub) -> MotionSensorX: | ||
return MotionSensorX(dirigeraClient=dirigera_client, **data) | ||
|
||
class HubX(Hub): | ||
def __init__( self, token: str, ip_address: str, port: str = "8443", api_version: str = "v1") -> None: | ||
super().__init__(token, ip_address, port, api_version) | ||
|
||
def get_motion_sensors(self) -> List[MotionSensor]: | ||
devices = self.get("/devices") | ||
sensors = list(filter(lambda x: x["deviceType"] == "motionSensor", devices)) | ||
return [dict_to_motion_sensorx(sensor, self) for sensor in sensors] | ||
|
||
def get_motion_sensor_by_id(self, id_: str) -> MotionSensorX: | ||
motion_sensor = self._get_device_data_by_id(id_) | ||
if motion_sensor["deviceType"] != "motionSensor": | ||
raise ValueError("Device is not an MotionSensor") | ||
return dict_to_motion_sensorx(motion_sensor, self) | ||
|
||
def get_open_close_by_id(self, id_: str) -> OpenCloseSensor: | ||
open_close_sensor = self._get_device_data_by_id(id_) | ||
if open_close_sensor["deviceType"] != "openCloseSensor": | ||
raise ValueError("Device is not an OpenCloseSensor") | ||
return dict_to_open_close_sensor(open_close_sensor, self) |
Oops, something went wrong.