2018-08-21 19:25:16 +00:00
|
|
|
"""Generic device for the HomematicIP Cloud component."""
|
2018-07-06 21:05:34 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2018-08-21 19:25:16 +00:00
|
|
|
ATTR_CONNECTED = 'connected'
|
2018-07-06 21:05:34 +00:00
|
|
|
ATTR_DEVICE_ID = 'device_id'
|
|
|
|
ATTR_DEVICE_LABEL = 'device_label'
|
2018-08-21 19:25:16 +00:00
|
|
|
ATTR_DEVICE_RSSI = 'device_rssi'
|
|
|
|
ATTR_DUTY_CYCLE = 'duty_cycle'
|
2018-07-06 21:05:34 +00:00
|
|
|
ATTR_FIRMWARE_STATE = 'firmware_state'
|
2018-08-21 19:25:16 +00:00
|
|
|
ATTR_GROUP_TYPE = 'group_type'
|
|
|
|
ATTR_HOME_ID = 'home_id'
|
|
|
|
ATTR_HOME_NAME = 'home_name'
|
2018-07-06 21:05:34 +00:00
|
|
|
ATTR_LOW_BATTERY = 'low_battery'
|
|
|
|
ATTR_MODEL_TYPE = 'model_type'
|
|
|
|
ATTR_OPERATION_LOCK = 'operation_lock'
|
2018-08-21 19:25:16 +00:00
|
|
|
ATTR_SABOTAGE = 'sabotage'
|
|
|
|
ATTR_STATUS_UPDATE = 'status_update'
|
|
|
|
ATTR_UNREACHABLE = 'unreachable'
|
2018-07-06 21:05:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HomematicipGenericDevice(Entity):
|
|
|
|
"""Representation of an HomematicIP generic device."""
|
|
|
|
|
|
|
|
def __init__(self, home, device, post=None):
|
|
|
|
"""Initialize the generic device."""
|
|
|
|
self._home = home
|
|
|
|
self._device = device
|
|
|
|
self.post = post
|
2018-08-21 19:25:16 +00:00
|
|
|
_LOGGER.info("Setting up %s (%s)", self.name, self._device.modelType)
|
2018-07-06 21:05:34 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
|
|
|
self._device.on_update(self._device_changed)
|
|
|
|
|
2018-09-30 20:39:25 +00:00
|
|
|
def _device_changed(self, *args, **kwargs):
|
2018-07-06 21:05:34 +00:00
|
|
|
"""Handle device state changes."""
|
2018-08-21 19:25:16 +00:00
|
|
|
_LOGGER.debug("Event %s (%s)", self.name, self._device.modelType)
|
2018-07-06 21:05:34 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the generic device."""
|
|
|
|
name = self._device.label
|
2018-08-21 19:25:16 +00:00
|
|
|
if self._home.name is not None and self._home.name != '':
|
2018-07-06 21:05:34 +00:00
|
|
|
name = "{} {}".format(self._home.name, name)
|
2018-08-21 19:25:16 +00:00
|
|
|
if self.post is not None and self.post != '':
|
2018-07-06 21:05:34 +00:00
|
|
|
name = "{} {}".format(name, self.post)
|
|
|
|
return name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Device available."""
|
|
|
|
return not self._device.unreach
|
|
|
|
|
2018-09-25 08:15:03 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return "{}_{}".format(self.__class__.__name__, self._device.id)
|
|
|
|
|
2018-07-18 10:19:08 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
|
|
|
if hasattr(self._device, 'lowBat') and self._device.lowBat:
|
|
|
|
return 'mdi:battery-outline'
|
|
|
|
if hasattr(self._device, 'sabotage') and self._device.sabotage:
|
|
|
|
return 'mdi:alert'
|
|
|
|
return None
|
|
|
|
|
2018-07-06 21:05:34 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the generic device."""
|
2018-07-18 10:19:08 +00:00
|
|
|
attr = {ATTR_MODEL_TYPE: self._device.modelType}
|
|
|
|
if hasattr(self._device, 'lowBat') and self._device.lowBat:
|
|
|
|
attr.update({ATTR_LOW_BATTERY: self._device.lowBat})
|
|
|
|
if hasattr(self._device, 'sabotage') and self._device.sabotage:
|
|
|
|
attr.update({ATTR_SABOTAGE: self._device.sabotage})
|
|
|
|
return attr
|