diff --git a/.coveragerc b/.coveragerc index dd3874a9ffd3bf45a1f4df258dab29097522fefe..f609b5cb053dd2e3ff372d734d205f2203920072 100644 --- a/.coveragerc +++ b/.coveragerc @@ -80,6 +80,9 @@ omit = homeassistant/components/hdmi_cec.py homeassistant/components/*/hdmi_cec.py + homeassistant/components/hive.py + homeassistant/components/*/hive.py + homeassistant/components/homematic.py homeassistant/components/*/homematic.py diff --git a/homeassistant/components/binary_sensor/hive.py b/homeassistant/components/binary_sensor/hive.py new file mode 100644 index 0000000000000000000000000000000000000000..b62c003c4fd161b48bf4d369d1deec8fc2cf2c03 --- /dev/null +++ b/homeassistant/components/binary_sensor/hive.py @@ -0,0 +1,63 @@ +""" +Support for the Hive devices. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/binary_sensor.hive/ +""" +from homeassistant.components.binary_sensor import BinarySensorDevice +from homeassistant.components.hive import DATA_HIVE + +DEPENDENCIES = ['hive'] + +DEVICETYPE_DEVICE_CLASS = {'motionsensor': 'motion', + 'contactsensor': 'opening'} + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up Hive sensor devices.""" + if discovery_info is None: + return + session = hass.data.get(DATA_HIVE) + + add_devices([HiveBinarySensorEntity(session, discovery_info)]) + + +class HiveBinarySensorEntity(BinarySensorDevice): + """Representation of a Hive binary sensor.""" + + def __init__(self, hivesession, hivedevice): + """Initialize the hive sensor.""" + self.node_id = hivedevice["Hive_NodeID"] + self.node_name = hivedevice["Hive_NodeName"] + self.device_type = hivedevice["HA_DeviceType"] + self.node_device_type = hivedevice["Hive_DeviceType"] + self.session = hivesession + self.data_updatesource = '{}.{}'.format(self.device_type, + self.node_id) + + self.session.entities.append(self) + + def handle_update(self, updatesource): + """Handle the new update request.""" + if '{}.{}'.format(self.device_type, self.node_id) not in updatesource: + self.schedule_update_ha_state() + + @property + def device_class(self): + """Return the class of this sensor.""" + return DEVICETYPE_DEVICE_CLASS.get(self.node_device_type) + + @property + def name(self): + """Return the name of the binary sensor.""" + return self.node_name + + @property + def is_on(self): + """Return true if the binary sensor is on.""" + return self.session.sensor.get_state(self.node_id, + self.node_device_type) + + def update(self): + """Update all Node data frome Hive.""" + self.session.core.update_data(self.node_id) diff --git a/homeassistant/components/climate/hive.py b/homeassistant/components/climate/hive.py new file mode 100644 index 0000000000000000000000000000000000000000..18833558b4485a8c86d7061b4f1cfcc0c273f7dc --- /dev/null +++ b/homeassistant/components/climate/hive.py @@ -0,0 +1,132 @@ +""" +Support for the Hive devices. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/climate.hive/ +""" +from homeassistant.components.climate import (ClimateDevice, + STATE_AUTO, STATE_HEAT, + STATE_OFF, STATE_ON) +from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS +from homeassistant.components.hive import DATA_HIVE + +DEPENDENCIES = ['hive'] +HIVE_TO_HASS_STATE = {'SCHEDULE': STATE_AUTO, 'MANUAL': STATE_HEAT, + 'ON': STATE_ON, 'OFF': STATE_OFF} +HASS_TO_HIVE_STATE = {STATE_AUTO: 'SCHEDULE', STATE_HEAT: 'MANUAL', + STATE_ON: 'ON', STATE_OFF: 'OFF'} + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up Hive climate devices.""" + if discovery_info is None: + return + session = hass.data.get(DATA_HIVE) + + add_devices([HiveClimateEntity(session, discovery_info)]) + + +class HiveClimateEntity(ClimateDevice): + """Hive Climate Device.""" + + def __init__(self, hivesession, hivedevice): + """Initialize the Climate device.""" + self.node_id = hivedevice["Hive_NodeID"] + self.node_name = hivedevice["Hive_NodeName"] + self.device_type = hivedevice["HA_DeviceType"] + self.session = hivesession + self.data_updatesource = '{}.{}'.format(self.device_type, + self.node_id) + + if self.device_type == "Heating": + self.modes = [STATE_AUTO, STATE_HEAT, STATE_OFF] + elif self.device_type == "HotWater": + self.modes = [STATE_AUTO, STATE_ON, STATE_OFF] + + self.session.entities.append(self) + + def handle_update(self, updatesource): + """Handle the new update request.""" + if '{}.{}'.format(self.device_type, self.node_id) not in updatesource: + self.schedule_update_ha_state() + + @property + def name(self): + """Return the name of the Climate device.""" + friendly_name = "Climate Device" + if self.device_type == "Heating": + friendly_name = "Heating" + if self.node_name is not None: + friendly_name = '{} {}'.format(self.node_name, friendly_name) + elif self.device_type == "HotWater": + friendly_name = "Hot Water" + return friendly_name + + @property + def temperature_unit(self): + """Return the unit of measurement.""" + return TEMP_CELSIUS + + @property + def current_temperature(self): + """Return the current temperature.""" + if self.device_type == "Heating": + return self.session.heating.current_temperature(self.node_id) + + @property + def target_temperature(self): + """Return the target temperature.""" + if self.device_type == "Heating": + return self.session.heating.get_target_temperature(self.node_id) + + @property + def min_temp(self): + """Return minimum temperature.""" + if self.device_type == "Heating": + return self.session.heating.min_temperature(self.node_id) + + @property + def max_temp(self): + """Return the maximum temperature.""" + if self.device_type == "Heating": + return self.session.heating.max_temperature(self.node_id) + + @property + def operation_list(self): + """List of the operation modes.""" + return self.modes + + @property + def current_operation(self): + """Return current mode.""" + if self.device_type == "Heating": + currentmode = self.session.heating.get_mode(self.node_id) + elif self.device_type == "HotWater": + currentmode = self.session.hotwater.get_mode(self.node_id) + return HIVE_TO_HASS_STATE.get(currentmode) + + def set_operation_mode(self, operation_mode): + """Set new Heating mode.""" + new_mode = HASS_TO_HIVE_STATE.get(operation_mode) + if self.device_type == "Heating": + self.session.heating.set_mode(self.node_id, new_mode) + elif self.device_type == "HotWater": + self.session.hotwater.set_mode(self.node_id, new_mode) + + for entity in self.session.entities: + entity.handle_update(self.data_updatesource) + + def set_temperature(self, **kwargs): + """Set new target temperature.""" + new_temperature = kwargs.get(ATTR_TEMPERATURE) + if new_temperature is not None: + if self.device_type == "Heating": + self.session.heating.set_target_temperature(self.node_id, + new_temperature) + + for entity in self.session.entities: + entity.handle_update(self.data_updatesource) + + def update(self): + """Update all Node data frome Hive.""" + self.session.core.update_data(self.node_id) diff --git a/homeassistant/components/hive.py b/homeassistant/components/hive.py new file mode 100644 index 0000000000000000000000000000000000000000..277800502c15f2ca0297706f2e78a69b15bd889c --- /dev/null +++ b/homeassistant/components/hive.py @@ -0,0 +1,80 @@ +""" +Support for the Hive devices. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/hive/ +""" +import logging +import voluptuous as vol + +from homeassistant.const import (CONF_PASSWORD, CONF_SCAN_INTERVAL, + CONF_USERNAME) +import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.discovery import load_platform + +REQUIREMENTS = ['pyhiveapi==0.2.5'] + +_LOGGER = logging.getLogger(__name__) +DOMAIN = 'hive' +DATA_HIVE = 'data_hive' +DEVICETYPES = { + 'binary_sensor': 'device_list_binary_sensor', + 'climate': 'device_list_climate', + 'light': 'device_list_light', + 'switch': 'device_list_plug', + 'sensor': 'device_list_sensor', + } + +CONFIG_SCHEMA = vol.Schema({ + DOMAIN: vol.Schema({ + vol.Required(CONF_PASSWORD): cv.string, + vol.Required(CONF_USERNAME): cv.string, + vol.Optional(CONF_SCAN_INTERVAL, default=2): cv.positive_int, + }) +}, extra=vol.ALLOW_EXTRA) + + +class HiveSession: + """Initiate Hive Session Class.""" + + entities = [] + core = None + heating = None + hotwater = None + light = None + sensor = None + switch = None + + +def setup(hass, config): + """Set up the Hive Component.""" + from pyhiveapi import Pyhiveapi + + session = HiveSession() + session.core = Pyhiveapi() + + username = config[DOMAIN][CONF_USERNAME] + password = config[DOMAIN][CONF_PASSWORD] + update_interval = config[DOMAIN][CONF_SCAN_INTERVAL] + + devicelist = session.core.initialise_api(username, + password, + update_interval) + + if devicelist is None: + _LOGGER.error("Hive API initialization failed") + return False + + session.sensor = Pyhiveapi.Sensor() + session.heating = Pyhiveapi.Heating() + session.hotwater = Pyhiveapi.Hotwater() + session.light = Pyhiveapi.Light() + session.switch = Pyhiveapi.Switch() + hass.data[DATA_HIVE] = session + + for ha_type, hive_type in DEVICETYPES.items(): + for key, devices in devicelist.items(): + if key == hive_type: + for hivedevice in devices: + load_platform(hass, ha_type, DOMAIN, hivedevice, config) + return True diff --git a/homeassistant/components/light/hive.py b/homeassistant/components/light/hive.py new file mode 100644 index 0000000000000000000000000000000000000000..95bd0b6988d2075a9fec54bf2c65725ab4404564 --- /dev/null +++ b/homeassistant/components/light/hive.py @@ -0,0 +1,126 @@ +""" +Support for the Hive devices. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/light.hive/ +""" +from homeassistant.components.hive import DATA_HIVE +from homeassistant.components.light import (ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, + SUPPORT_BRIGHTNESS, + SUPPORT_COLOR_TEMP, + SUPPORT_RGB_COLOR, Light) + +DEPENDENCIES = ['hive'] + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up Hive light devices.""" + if discovery_info is None: + return + session = hass.data.get(DATA_HIVE) + + add_devices([HiveDeviceLight(session, discovery_info)]) + + +class HiveDeviceLight(Light): + """Hive Active Light Device.""" + + def __init__(self, hivesession, hivedevice): + """Initialize the Light device.""" + self.node_id = hivedevice["Hive_NodeID"] + self.node_name = hivedevice["Hive_NodeName"] + self.device_type = hivedevice["HA_DeviceType"] + self.light_device_type = hivedevice["Hive_Light_DeviceType"] + self.session = hivesession + self.data_updatesource = '{}.{}'.format(self.device_type, + self.node_id) + self.session.entities.append(self) + + def handle_update(self, updatesource): + """Handle the new update request.""" + if '{}.{}'.format(self.device_type, self.node_id) not in updatesource: + self.schedule_update_ha_state() + + @property + def name(self): + """Return the display name of this light.""" + return self.node_name + + @property + def min_mireds(self): + """Return the coldest color_temp that this light supports.""" + if self.light_device_type == "tuneablelight" \ + or self.light_device_type == "colourtuneablelight": + return self.session.light.get_min_colour_temp(self.node_id) + + @property + def max_mireds(self): + """Return the warmest color_temp that this light supports.""" + if self.light_device_type == "tuneablelight" \ + or self.light_device_type == "colourtuneablelight": + return self.session.light.get_max_colour_temp(self.node_id) + + @property + def color_temp(self): + """Return the CT color value in mireds.""" + if self.light_device_type == "tuneablelight" \ + or self.light_device_type == "colourtuneablelight": + return self.session.light.get_color_temp(self.node_id) + + @property + def brightness(self): + """Brightness of the light (an integer in the range 1-255).""" + return self.session.light.get_brightness(self.node_id) + + @property + def is_on(self): + """Return true if light is on.""" + return self.session.light.get_state(self.node_id) + + def turn_on(self, **kwargs): + """Instruct the light to turn on.""" + new_brightness = None + new_color_temp = None + if ATTR_BRIGHTNESS in kwargs: + tmp_new_brightness = kwargs.get(ATTR_BRIGHTNESS) + percentage_brightness = ((tmp_new_brightness / 255) * 100) + new_brightness = int(round(percentage_brightness / 5.0) * 5.0) + if new_brightness == 0: + new_brightness = 5 + if ATTR_COLOR_TEMP in kwargs: + tmp_new_color_temp = kwargs.get(ATTR_COLOR_TEMP) + new_color_temp = round(1000000 / tmp_new_color_temp) + + if new_brightness is not None: + self.session.light.set_brightness(self.node_id, new_brightness) + elif new_color_temp is not None: + self.session.light.set_colour_temp(self.node_id, new_color_temp) + else: + self.session.light.turn_on(self.node_id) + + for entity in self.session.entities: + entity.handle_update(self.data_updatesource) + + def turn_off(self): + """Instruct the light to turn off.""" + self.session.light.turn_off(self.node_id) + for entity in self.session.entities: + entity.handle_update(self.data_updatesource) + + @property + def supported_features(self): + """Flag supported features.""" + supported_features = None + if self.light_device_type == "warmwhitelight": + supported_features = SUPPORT_BRIGHTNESS + elif self.light_device_type == "tuneablelight": + supported_features = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP) + elif self.light_device_type == "colourtuneablelight": + supported_features = ( + SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_RGB_COLOR) + + return supported_features + + def update(self): + """Update all Node data frome Hive.""" + self.session.core.update_data(self.node_id) diff --git a/homeassistant/components/sensor/hive.py b/homeassistant/components/sensor/hive.py new file mode 100644 index 0000000000000000000000000000000000000000..ce07dfdda5a8410d86c163aaf9df1938bdab1ea9 --- /dev/null +++ b/homeassistant/components/sensor/hive.py @@ -0,0 +1,52 @@ +""" +Support for the Hive devices. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/sensor.hive/ +""" +from homeassistant.components.hive import DATA_HIVE +from homeassistant.helpers.entity import Entity + +DEPENDENCIES = ['hive'] + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up Hive sensor devices.""" + if discovery_info is None: + return + session = hass.data.get(DATA_HIVE) + + if discovery_info["HA_DeviceType"] == "Hub_OnlineStatus": + add_devices([HiveSensorEntity(session, discovery_info)]) + + +class HiveSensorEntity(Entity): + """Hive Sensor Entity.""" + + def __init__(self, hivesession, hivedevice): + """Initialize the sensor.""" + self.node_id = hivedevice["Hive_NodeID"] + self.device_type = hivedevice["HA_DeviceType"] + self.session = hivesession + self.data_updatesource = '{}.{}'.format(self.device_type, + self.node_id) + self.session.entities.append(self) + + def handle_update(self, updatesource): + """Handle the new update request.""" + if '{}.{}'.format(self.device_type, self.node_id) not in updatesource: + self.schedule_update_ha_state() + + @property + def name(self): + """Return the name of the sensor.""" + return "Hive hub status" + + @property + def state(self): + """Return the state of the sensor.""" + return self.session.sensor.hub_online_status(self.node_id) + + def update(self): + """Update all Node data frome Hive.""" + self.session.core.update_data(self.node_id) diff --git a/homeassistant/components/switch/hive.py b/homeassistant/components/switch/hive.py new file mode 100644 index 0000000000000000000000000000000000000000..d77247a5c04b1afa9146db643a2f9e4a0577e0a8 --- /dev/null +++ b/homeassistant/components/switch/hive.py @@ -0,0 +1,69 @@ +""" +Support for the Hive devices. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/switch.hive/ +""" +from homeassistant.components.switch import SwitchDevice +from homeassistant.components.hive import DATA_HIVE + +DEPENDENCIES = ['hive'] + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up Hive switches.""" + if discovery_info is None: + return + session = hass.data.get(DATA_HIVE) + + add_devices([HiveDevicePlug(session, discovery_info)]) + + +class HiveDevicePlug(SwitchDevice): + """Hive Active Plug.""" + + def __init__(self, hivesession, hivedevice): + """Initialize the Switch device.""" + self.node_id = hivedevice["Hive_NodeID"] + self.node_name = hivedevice["Hive_NodeName"] + self.device_type = hivedevice["HA_DeviceType"] + self.session = hivesession + self.data_updatesource = '{}.{}'.format(self.device_type, + self.node_id) + self.session.entities.append(self) + + def handle_update(self, updatesource): + """Handle the new update request.""" + if '{}.{}'.format(self.device_type, self.node_id) not in updatesource: + self.schedule_update_ha_state() + + @property + def name(self): + """Return the name of this Switch device if any.""" + return self.node_name + + @property + def current_power_w(self): + """Return the current power usage in W.""" + return self.session.switch.get_power_usage(self.node_id) + + @property + def is_on(self): + """Return true if switch is on.""" + return self.session.switch.get_state(self.node_id) + + def turn_on(self, **kwargs): + """Turn the switch on.""" + self.session.switch.turn_on(self.node_id) + for entity in self.session.entities: + entity.handle_update(self.data_updatesource) + + def turn_off(self, **kwargs): + """Turn the device off.""" + self.session.switch.turn_off(self.node_id) + for entity in self.session.entities: + entity.handle_update(self.data_updatesource) + + def update(self): + """Update all Node data frome Hive.""" + self.session.core.update_data(self.node_id) diff --git a/requirements_all.txt b/requirements_all.txt index cff019312ef105921450c482641b064606168e69..7ec77dffcf7f663e624590d981e24af73b67d1c4 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -667,6 +667,9 @@ pyharmony==1.0.18 # homeassistant.components.binary_sensor.hikvision pyhik==0.1.4 +# homeassistant.components.hive +pyhiveapi==0.2.5 + # homeassistant.components.homematic pyhomematic==0.1.34