Skip to content
Snippets Groups Projects
Commit 1beaf82c authored by jkerdreu's avatar jkerdreu
Browse files

- refactoring async_setup_entry()

- refactoring sensors / binary sensors


git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@2986 b32b6428-25c9-4566-ad07-03861ab6144f
parent 91ab1a60
No related branches found
No related tags found
No related merge requests found
from xaal.lib import helpers
import logging
import functools
from homeassistant.components.binary_sensor import BinarySensorEntity, BinarySensorDeviceClass
from homeassistant.const import STATE_ON, STATE_OFF
from .const import DOMAIN
from .core import XAALEntity, EntityFactory
from .core import XAALEntity, EntityFactory, async_setup_factory
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities):
return async_setup_factory(hass, config_entry, async_add_entities, Factory)
class Factory(EntityFactory):
......@@ -34,55 +34,40 @@ class Factory(EntityFactory):
return False
async def async_setup_entry(hass, config_entry, async_add_entities):
bridge = hass.data[DOMAIN][config_entry.entry_id]
factory = Factory(bridge, async_add_entities)
for dev in bridge._mon.devices:
if dev.is_ready():
factory.new_entity(dev)
class Motion(XAALEntity, BinarySensorEntity):
_attr_device_class = BinarySensorDeviceClass.MOTION
class XAALBinarySensorEntity(XAALEntity, BinarySensorEntity):
@property
def state(self):
try:
attr = getattr(self,'_xaal_attribute')
value = self._dev.attributes.get('presence', None)
return STATE_ON if value else STATE_OFF
except:
return None
class Contact(XAALEntity, BinarySensorEntity):
_attr_device_class = BinarySensorDeviceClass.OPENING
@property
def state(self):
value = self._dev.attributes.get('detected', None)
# return STATE_OPEN if value else STATE_CLOSED
return STATE_ON if value else STATE_OFF
class Motion(XAALBinarySensorEntity):
_attr_device_class = BinarySensorDeviceClass.MOTION
_xaal_attribute = 'presence'
class Switch(XAALEntity, BinarySensorEntity):
_attr_device_class = BinarySensorDeviceClass.POWER
class Contact(XAALBinarySensorEntity):
_attr_device_class = BinarySensorDeviceClass.OPENING
_xaal_attribute = 'detected'
@property
def state(self):
value = self._dev.attributes.get('position', None)
# return STATE_OPEN if value else STATE_CLOSED
return STATE_ON if value else STATE_OFF
class Switch(XAALBinarySensorEntity):
_attr_device_class = BinarySensorDeviceClass.POWER
_xaal_attribute = 'position'
class Button(XAALEntity, BinarySensorEntity):
@property
def state(self):
return False
class Button(XAALBinarySensorEntity):
def click_event(self, click_type):
# TODO change this sig, to hande several button types..
_LOGGER.warning(f"Button event: {self.entity_id}")
self.hass.bus.fire("xaal_event", {'entity_id': self.entity_id, "click_type": click_type})
def handle_notification(self, msg):
if msg.action == 'click':
self.click_event('single')
......
......@@ -3,6 +3,7 @@ from .const import DOMAIN
from homeassistant.helpers.entity import Entity
import logging
_LOGGER = logging.getLogger(__name__)
......@@ -77,3 +78,11 @@ class EntityFactory(object):
def add_entity(self, entity, address):
self._async_add_entitites([entity])
self._bridge.add_entity(address, entity)
def async_setup_factory(hass, config_entry, async_add_entities, factory_class):
bridge = hass.data[DOMAIN][config_entry.entry_id]
factory = factory_class(bridge, async_add_entities)
for dev in bridge._mon.devices:
if dev.is_ready():
factory.new_entity(dev)
import logging
from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_HS_COLOR, ATTR_COLOR_TEMP, LightEntity)
from homeassistant.components.light import ATTR_BRIGHTNESS, ATTR_HS_COLOR, ATTR_COLOR_TEMP, LightEntity
from homeassistant.util import color as color_util
from .const import DOMAIN
from .core import XAALEntity, EntityFactory
from .core import XAALEntity, EntityFactory, async_setup_factory
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities):
return async_setup_factory(hass, config_entry, async_add_entities, Factory)
class Factory(EntityFactory):
def new_entity(self, device):
......@@ -20,14 +22,6 @@ class Factory(EntityFactory):
return False
async def async_setup_entry(hass, config_entry, async_add_entities):
bridge = hass.data[DOMAIN][config_entry.entry_id]
factory = Factory(bridge, async_add_entities)
for dev in bridge._mon.devices:
if dev.is_ready():
factory.new_entity(dev)
class Lamp(XAALEntity, LightEntity):
@property
......
import logging
from homeassistant.components.sensor import SensorEntity, SensorDeviceClass
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.const import (
TEMP_CELSIUS,
......@@ -8,13 +8,16 @@ from homeassistant.const import (
POWER_WATT,
)
from .const import DOMAIN
from .core import XAALEntity, EntityFactory
from .core import XAALEntity, EntityFactory, async_setup_factory
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities):
return async_setup_factory(hass, config_entry, async_add_entities, Factory)
class Factory(EntityFactory):
def new_entity(self, device):
......@@ -40,53 +43,39 @@ class Factory(EntityFactory):
return False
async def async_setup_entry(hass, config_entry, async_add_entities):
bridge = hass.data[DOMAIN][config_entry.entry_id]
factory = Factory(bridge, async_add_entities)
for dev in bridge._mon.devices:
if dev.is_ready():
factory.new_entity(dev)
class XAALSensorEntity(XAALEntity, SensorEntity):
@property
def native_value(self):
target = getattr(self,'_xaal_attribute')
return self._dev.attributes.get(target,None)
class Thermometer(XAALEntity, SensorEntity):
class Thermometer(XAALSensorEntity):
_attr_device_class = SensorDeviceClass.TEMPERATURE
_attr_native_unit_of_measurement = TEMP_CELSIUS
@property
def state(self):
return self._dev.attributes.get('temperature', None)
_xaal_attribute = 'temperature'
class Hygrometer(XAALEntity, SensorEntity):
class Hygrometer(XAALSensorEntity):
_attr_device_class = SensorDeviceClass.HUMIDITY
_attr_native_unit_of_measurement = PERCENTAGE
@property
def state(self):
return self._dev.attributes.get('humidity', None)
_xaal_attribute = 'humidity'
class Battery(XAALEntity, SensorEntity):
class Battery(XAALSensorEntity):
_attr_device_class = SensorDeviceClass.BATTERY
_attr_unit_of_measurement = PERCENTAGE
@property
def state(self):
return self._dev.attributes.get('level', None)
_xaal_attribute = 'level'
class PowerMeter(XAALEntity, SensorEntity):
class PowerMeter(XAALSensorEntity):
_attr_device_class = SensorDeviceClass.POWER
_attr_native_unit_of_measurement = POWER_WATT
@property
def state(self):
return self._dev.attributes.get('power', None)
_xaal_attribute = 'power'
class WifiMeter(XAALEntity, SensorEntity):
class WifiMeter(XAALSensorEntity):
_attr_device_class: SensorDeviceClass.SIGNAL_STRENGTH
_xaal_attribute = 'rssi'
@property
def state(self):
return self._dev.attributes.get('rssi', None)
......@@ -2,12 +2,15 @@ import logging
from homeassistant.components.switch import SwitchEntity, DEVICE_CLASS_OUTLET
from .const import DOMAIN
from .core import EntityFactory, XAALEntity
from .core import EntityFactory, XAALEntity, async_setup_factory
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities):
return async_setup_factory(hass, config_entry, async_add_entities, Factory)
class Factory(EntityFactory):
def new_entity(self, device):
......@@ -17,13 +20,6 @@ class Factory(EntityFactory):
return True
return False
async def async_setup_entry(hass, config_entry, async_add_entities):
bridge = hass.data[DOMAIN][config_entry.entry_id]
factory = Factory(bridge, async_add_entities)
for dev in bridge._mon.devices:
if dev.is_ready():
factory.new_entity(dev)
class PowerRelay(XAALEntity, SwitchEntity):
_attr_device_class = DEVICE_CLASS_OUTLET
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment