-
-
Notifications
You must be signed in to change notification settings - Fork 38.2k
Ihc component and platforms #10916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ihc component and platforms #10916
Changes from 29 commits
b81abe9
4f6abc6
9afff5a
97986c4
28e26c7
db2fa5a
ce17c41
1b2a18f
471bbf0
c6a9d50
0b2a95f
4f3d821
35308e7
38108ec
3a14261
d533156
241770b
84fc69e
8b4ed8b
0edc3a4
df3fbed
ec971d1
be1a9aa
f93a617
12c6dfa
7bb0bfd
1aa6a2a
0fda18d
ba0ff92
5cfee95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| """IHC binary sensor platform. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/binary_sensor.ihc/ | ||
| """ | ||
| from xml.etree.ElementTree import Element | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.components.binary_sensor import ( | ||
| BinarySensorDevice, PLATFORM_SCHEMA, DEVICE_CLASSES_SCHEMA) | ||
| from homeassistant.components.ihc import ( | ||
| validate_name, IHC_DATA, IHC_CONTROLLER) | ||
| from homeassistant.components.ihc.const import CONF_INVERTING | ||
| from homeassistant.components.ihc.ihcdevice import IHCDevice | ||
| from homeassistant.const import ( | ||
| CONF_NAME, CONF_TYPE, CONF_ID, CONF_BINARY_SENSORS) | ||
| import homeassistant.helpers.config_validation as cv | ||
|
|
||
| DEPENDENCIES = ['ihc'] | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Optional(CONF_BINARY_SENSORS, default=[]): | ||
| vol.All(cv.ensure_list, [ | ||
| vol.All({ | ||
| vol.Required(CONF_ID): cv.positive_int, | ||
| vol.Optional(CONF_NAME): cv.string, | ||
| vol.Optional(CONF_TYPE, default=None): DEVICE_CLASSES_SCHEMA, | ||
| vol.Optional(CONF_INVERTING, default=False): cv.boolean, | ||
| }, validate_name) | ||
| ]) | ||
| }) | ||
|
|
||
|
|
||
| def setup_platform(hass, config, add_devices, discovery_info=None): | ||
| """Set up the IHC binary setsor platform.""" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| ihc_controller = hass.data[IHC_DATA][IHC_CONTROLLER] | ||
| devices = [] | ||
| if discovery_info: | ||
| for name, device in discovery_info.items(): | ||
| ihc_id = device['ihc_id'] | ||
| product_cfg = device['product_cfg'] | ||
| product = device['product'] | ||
| sensor = IHCBinarySensor(ihc_controller, name, ihc_id, | ||
| product_cfg[CONF_TYPE], | ||
| product_cfg[CONF_INVERTING], | ||
| product) | ||
| devices.append(sensor) | ||
| else: | ||
| binary_sensors = config[CONF_BINARY_SENSORS] | ||
| for sensor_cfg in binary_sensors: | ||
| ihc_id = sensor_cfg[CONF_ID] | ||
| name = sensor_cfg[CONF_NAME] | ||
| sensor_type = sensor_cfg[CONF_TYPE] | ||
| inverting = sensor_cfg[CONF_INVERTING] | ||
| sensor = IHCBinarySensor(ihc_controller, name, ihc_id, | ||
| sensor_type, inverting) | ||
| devices.append(sensor) | ||
|
|
||
| add_devices(devices) | ||
|
|
||
|
|
||
| class IHCBinarySensor(IHCDevice, BinarySensorDevice): | ||
| """IHC Binary Sensor. | ||
|
|
||
| The associated IHC resource can be any in or output from a IHC product | ||
| or function block, but it must be a boolean ON/OFF resources. | ||
| """ | ||
|
|
||
| def __init__(self, ihc_controller, name, ihc_id, sensor_type: str, | ||
| inverting: bool, product: Element=None): | ||
| """Initialize the IHC binary sensor.""" | ||
| super().__init__(ihc_controller, name, ihc_id, product) | ||
| self._state = None | ||
| self._sensor_type = sensor_type | ||
| self.inverting = inverting | ||
|
|
||
| @property | ||
| def device_class(self): | ||
| """Return the class of this sensor.""" | ||
| return self._sensor_type | ||
|
|
||
| @property | ||
| def is_on(self): | ||
| """Return true if the binary sensor is on/open.""" | ||
| return self._state | ||
|
|
||
| def on_ihc_change(self, ihc_id, value): | ||
| """IHC resource has changed.""" | ||
| if self.inverting: | ||
| self._state = not value | ||
| else: | ||
| self._state = value | ||
| self.schedule_update_ha_state() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| """IHC component. | ||
|
|
||
| For more details about this component, please refer to the documentation at | ||
| https://home-assistant.io/components/ihc/ | ||
| """ | ||
| import logging | ||
| import os.path | ||
| import xml.etree.ElementTree | ||
| import voluptuous as vol | ||
| from voluptuous.error import Error as VoluptuousError | ||
|
|
||
| from homeassistant.components.ihc.const import ( | ||
| ATTR_IHC_ID, ATTR_VALUE, CONF_INFO, CONF_AUTOSETUP, | ||
| CONF_BINARY_SENSOR, CONF_LIGHT, CONF_SENSOR, CONF_SWITCH, | ||
| CONF_XPATH, CONF_NODE, CONF_DIMMABLE, CONF_INVERTING, | ||
| SERVICE_SET_RUNTIME_VALUE_BOOL, SERVICE_SET_RUNTIME_VALUE_INT, | ||
| SERVICE_SET_RUNTIME_VALUE_FLOAT) | ||
| from homeassistant.config import load_yaml_config_file | ||
| from homeassistant.const import ( | ||
| CONF_URL, CONF_USERNAME, CONF_PASSWORD, CONF_ID, CONF_NAME, | ||
| CONF_UNIT_OF_MEASUREMENT, CONF_TYPE) | ||
| from homeassistant.helpers import discovery | ||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.helpers.typing import HomeAssistantType | ||
|
|
||
| REQUIREMENTS = ['ihcsdk==2.1.1'] | ||
|
|
||
| DOMAIN = 'ihc' | ||
| IHC_DATA = 'ihc' | ||
| IHC_CONTROLLER = 'controller' | ||
| IHC_INFO = 'info' | ||
| AUTO_SETUP_YAML = 'ihc_auto_setup.yaml' | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema({ | ||
| DOMAIN: vol.Schema({ | ||
| vol.Required(CONF_URL): cv.string, | ||
| vol.Required(CONF_USERNAME): cv.string, | ||
| vol.Required(CONF_PASSWORD): cv.string, | ||
| vol.Optional(CONF_AUTOSETUP, default=True): cv.boolean, | ||
| vol.Optional(CONF_INFO, default=True): cv.boolean | ||
| }), | ||
| }, extra=vol.ALLOW_EXTRA) | ||
|
|
||
| AUTO_SETUP_SCHEMA = vol.Schema({ | ||
| vol.Optional(CONF_BINARY_SENSOR, default=[]): | ||
| vol.All(cv.ensure_list, [ | ||
| vol.All({ | ||
| vol.Required(CONF_XPATH): cv.string, | ||
| vol.Required(CONF_NODE): cv.string, | ||
| vol.Optional(CONF_TYPE, default=None): cv.string, | ||
| vol.Optional(CONF_INVERTING, default=False): cv.boolean, | ||
| }) | ||
| ]), | ||
| vol.Optional(CONF_LIGHT, default=[]): | ||
| vol.All(cv.ensure_list, [ | ||
| vol.All({ | ||
| vol.Required(CONF_XPATH): cv.string, | ||
| vol.Required(CONF_NODE): cv.string, | ||
| vol.Optional(CONF_DIMMABLE, default=False): cv.boolean, | ||
| }) | ||
| ]), | ||
| vol.Optional(CONF_SENSOR, default=[]): | ||
| vol.All(cv.ensure_list, [ | ||
| vol.All({ | ||
| vol.Required(CONF_XPATH): cv.string, | ||
| vol.Required(CONF_NODE): cv.string, | ||
| vol.Optional(CONF_TYPE, default='Temperature'): cv.string, | ||
| vol.Optional(CONF_UNIT_OF_MEASUREMENT, | ||
| default='°C'): cv.string, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| }) | ||
| ]), | ||
| vol.Optional(CONF_SWITCH, default=[]): | ||
| vol.All(cv.ensure_list, [ | ||
| vol.All({ | ||
| vol.Required(CONF_XPATH): cv.string, | ||
| vol.Required(CONF_NODE): cv.string, | ||
| }) | ||
| ]), | ||
| }) | ||
|
|
||
| SET_RUNTIME_VALUE_BOOL_SCHEMA = vol.Schema({ | ||
| vol.Required(ATTR_IHC_ID): cv.positive_int, | ||
| vol.Required(ATTR_VALUE): cv.boolean | ||
| }) | ||
|
|
||
| SET_RUNTIME_VALUE_INT_SCHEMA = vol.Schema({ | ||
| vol.Required(ATTR_IHC_ID): cv.positive_int, | ||
| vol.Required(ATTR_VALUE): int | ||
| }) | ||
|
|
||
| SET_RUNTIME_VALUE_FLOAT_SCHEMA = vol.Schema({ | ||
| vol.Required(ATTR_IHC_ID): cv.positive_int, | ||
| vol.Required(ATTR_VALUE): vol.Coerce(float) | ||
| }) | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| IHC_PLATFORMS = ('binary_sensor', 'light', 'sensor', 'switch') | ||
|
|
||
|
|
||
| def setup(hass, config): | ||
| """Setup the IHC component.""" | ||
| from ihcsdk.ihccontroller import IHCController | ||
| conf = config[DOMAIN] | ||
| url = conf[CONF_URL] | ||
| username = conf[CONF_USERNAME] | ||
| password = conf[CONF_PASSWORD] | ||
| ihc_controller = IHCController(url, username, password) | ||
|
|
||
| if not ihc_controller.authenticate(): | ||
| _LOGGER.error("Unable to authenticate on ihc controller.") | ||
| return False | ||
|
|
||
| if (conf[CONF_AUTOSETUP] and | ||
| not autosetup_ihc_products(hass, ihc_controller)): | ||
| return False | ||
|
|
||
| hass.data[IHC_DATA] = { | ||
| IHC_CONTROLLER: ihc_controller, | ||
| IHC_INFO: conf[CONF_INFO]} | ||
|
|
||
| setup_service_functions(hass, ihc_controller) | ||
| return True | ||
|
|
||
|
|
||
| def autosetup_ihc_products(hass: HomeAssistantType, ihc_controller): | ||
| """Auto setup of IHC products from the ihc project file.""" | ||
| project_xml = ihc_controller.get_project() | ||
| if not project_xml: | ||
| _LOGGER.error("Unable to read project from ihc controller.") | ||
| return False | ||
| project = xml.etree.ElementTree.fromstring(project_xml) | ||
|
|
||
| # if an auto setup file exist in the configuration it will override | ||
| yaml_path = hass.config.path(AUTO_SETUP_YAML) | ||
| if not os.path.isfile(yaml_path): | ||
| yaml_path = os.path.join(os.path.dirname(__file__), AUTO_SETUP_YAML) | ||
| yaml = load_yaml_config_file(yaml_path) | ||
| try: | ||
| auto_setup_conf = AUTO_SETUP_SCHEMA(yaml) | ||
| except VoluptuousError as exception: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. except vol.Invalid as exc:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The except VoluptuousError will catch all error from voluptuous (I copied it from somewhere else in home-assistant), but I assume your reason is that vol.Invalid is better because the schema is always valid (build into the code). I have changed it to vol.Invalid |
||
| _LOGGER.error("Invalid IHC auto setup data: %s", exception) | ||
| return False | ||
| groups = project.findall('.//group') | ||
| for component in IHC_PLATFORMS: | ||
| component_setup = auto_setup_conf[component] | ||
| discovery_info = get_discovery_info(component_setup, groups) | ||
| if len(discovery_info) > 0: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if discovery_info:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| discovery.load_platform(hass, component, DOMAIN, discovery_info) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the original config as fifth argument.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added the config. It is not used, and I can see other components also not passing the config, but I guess it is something like good practice in case it is need later. |
||
| return True | ||
|
|
||
|
|
||
| def get_discovery_info(component_setup, groups): | ||
| """Get discover info for specified component.""" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "discovery"
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| discovery_data = {} | ||
| for group in groups: | ||
| groupname = group.attrib['name'] | ||
| for product_cfg in component_setup: | ||
| products = group.findall(product_cfg[CONF_XPATH]) | ||
| for product in products: | ||
| nodes = product.findall(product_cfg[CONF_NODE]) | ||
| for node in nodes: | ||
| if ('setting' in node.attrib | ||
| and node.attrib['setting'] == 'yes'): | ||
| continue | ||
| ihc_id = int(node.attrib['id'].strip('_'), 0) | ||
| name = '{}_{}'.format(groupname, ihc_id) | ||
| device = { | ||
| 'ihc_id': ihc_id, | ||
| 'product': product, | ||
| 'product_cfg': product_cfg} | ||
| discovery_data[name] = device | ||
| return discovery_data | ||
|
|
||
|
|
||
| def setup_service_functions(hass: HomeAssistantType, ihc_controller): | ||
| """Setup the ihc service functions.""" | ||
| def set_runtime_value_bool(call): | ||
| """Set a IHC runtime bool value service function.""" | ||
| ihc_id = call.data[ATTR_IHC_ID] | ||
| value = call.data[ATTR_VALUE] | ||
| ihc_controller.set_runtime_value_bool(ihc_id, value) | ||
|
|
||
| def set_runtime_value_int(call): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking, instead of creating three services, maybe one service is enough that takes a type in the service data. But that might just make things more complex. You decide.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is important to have the type correct when your set a runtime value on the IHC controller. For example if you try to set a float value with an int, it will fail. So I prefer having separate functions for each type. And now when service data is validated (see below). it is even better. |
||
| """Set a IHC runtime integer value service function.""" | ||
| ihc_id = call.data[ATTR_IHC_ID] | ||
| value = call.data[ATTR_VALUE] | ||
| ihc_controller.set_runtime_value_int(ihc_id, value) | ||
|
|
||
| def set_runtime_value_float(call): | ||
| """Set a IHC runtime float value service function.""" | ||
| ihc_id = call.data[ATTR_IHC_ID] | ||
| value = call.data[ATTR_VALUE] | ||
| ihc_controller.set_runtime_value_float(ihc_id, value) | ||
|
|
||
| descriptions = load_yaml_config_file( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this. See below.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| os.path.join(os.path.dirname(__file__), 'services.yaml')) | ||
|
|
||
| hass.services.register(DOMAIN, SERVICE_SET_RUNTIME_VALUE_BOOL, | ||
| set_runtime_value_bool, | ||
| descriptions[SERVICE_SET_RUNTIME_VALUE_BOOL], | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After a core refactor we no longer load the descriptions and add them when registering services. So remove the description argument.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| schema=SET_RUNTIME_VALUE_BOOL_SCHEMA) | ||
| hass.services.register(DOMAIN, SERVICE_SET_RUNTIME_VALUE_INT, | ||
| set_runtime_value_int, | ||
| descriptions[SERVICE_SET_RUNTIME_VALUE_INT], | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| schema=SET_RUNTIME_VALUE_INT_SCHEMA) | ||
| hass.services.register(DOMAIN, SERVICE_SET_RUNTIME_VALUE_FLOAT, | ||
| set_runtime_value_float, | ||
| descriptions[SERVICE_SET_RUNTIME_VALUE_FLOAT], | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| schema=SET_RUNTIME_VALUE_FLOAT_SCHEMA) | ||
|
|
||
|
|
||
| def validate_name(config): | ||
| """Validate device name.""" | ||
| if CONF_NAME in config: | ||
| return config | ||
| ihcid = config[CONF_ID] | ||
| name = 'ihc_{}'.format(ihcid) | ||
| config[CONF_NAME] = name | ||
| return config | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| """IHC component constants.""" | ||
|
|
||
| CONF_AUTOSETUP = 'auto_setup' | ||
| CONF_INFO = 'info' | ||
| CONF_XPATH = 'xpath' | ||
| CONF_NODE = 'node' | ||
| CONF_INVERTING = 'inverting' | ||
| CONF_DIMMABLE = 'dimmable' | ||
| CONF_BINARY_SENSOR = 'binary_sensor' | ||
| CONF_LIGHT = 'light' | ||
| CONF_SENSOR = 'sensor' | ||
| CONF_SWITCH = 'switch' | ||
|
|
||
| ATTR_IHC_ID = 'ihc_id' | ||
| ATTR_VALUE = 'value' | ||
|
|
||
| SERVICE_SET_RUNTIME_VALUE_BOOL = "set_runtime_value_bool" | ||
| SERVICE_SET_RUNTIME_VALUE_INT = "set_runtime_value_int" | ||
| SERVICE_SET_RUNTIME_VALUE_FLOAT = "set_runtime_value_float" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please group and sort imports according to PEP8.
https://www.python.org/dev/peps/pep-0008/#imports
Sort the imports 🔡 within each group standard library, 3rd party and homeassistant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done