hass-core/homeassistant/components/ihc/ihcdevice.py
Jens Østergaard Nielsen e02d5e7ff1 Ihc component and platforms (#10916)
* Added IHC platform

* Updated requirements for IHC platform

* Exclude IHC from test

* Correcting flake8 issues

* Fixing more flake8 issues

* Fixed flake8 issues

* Fixing pylint issues

* Fixed flake8 issues

* Changes from PR review.

* STATE_UNKNOWN changed to None

* Spelling mistake in comment

* Added IHC platform

* Updated requirements for IHC platform

* Exclude IHC from test

* Correcting flake8 issues

* Fixing more flake8 issues

* Fixed flake8 issues

* Fixing pylint issues

* Fixed flake8 issues

* Changes from PR review.

* STATE_UNKNOWN changed to None

* Spelling mistake in comment

* Updated requirements_all.txt with gen_requirements_app.py

* Pylint fix: No space allowed around keyword argument assignment

* PR review changes

* Moved auto setup from platforms to ihc component

* Do no auto setup if there are no IHC products found

* Changes from PR review
2018-01-20 16:29:50 +01:00

65 lines
1.9 KiB
Python

"""Implements a base class for all IHC devices."""
import asyncio
from xml.etree.ElementTree import Element
from homeassistant.helpers.entity import Entity
class IHCDevice(Entity):
"""Base class for all ihc devices.
All IHC devices have an associated IHC resource. IHCDevice handled the
registration of the IHC controller callback when the IHC resource changes.
Derived classes must implement the on_ihc_change method
"""
def __init__(self, ihc_controller, name, ihc_id: int, info: bool,
product: Element=None):
"""Initialize IHC attributes."""
self.ihc_controller = ihc_controller
self._name = name
self.ihc_id = ihc_id
self.info = info
if product:
self.ihc_name = product.attrib['name']
self.ihc_note = product.attrib['note']
self.ihc_position = product.attrib['position']
else:
self.ihc_name = ''
self.ihc_note = ''
self.ihc_position = ''
@asyncio.coroutine
def async_added_to_hass(self):
"""Add callback for ihc changes."""
self.ihc_controller.add_notify_event(
self.ihc_id, self.on_ihc_change, True)
@property
def should_poll(self) -> bool:
"""No polling needed for ihc devices."""
return False
@property
def name(self):
"""Return the device name."""
return self._name
@property
def device_state_attributes(self):
"""Return the state attributes."""
if not self.info:
return {}
return {
'ihc_id': self.ihc_id,
'ihc_name': self.ihc_name,
'ihc_note': self.ihc_note,
'ihc_position': self.ihc_position
}
def on_ihc_change(self, ihc_id, value):
"""Callback when ihc resource changes.
Derived classes must overwrite this to do device specific stuff.
"""
raise NotImplementedError