diff --git a/homeassistant/components/binary_sensor/hive.py b/homeassistant/components/binary_sensor/hive.py index b62c003c4fd..6223ebe50a1 100644 --- a/homeassistant/components/binary_sensor/hive.py +++ b/homeassistant/components/binary_sensor/hive.py @@ -1,63 +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) +""" +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 index 8305e772869..b8ac66d91b3 100644 --- a/homeassistant/components/climate/hive.py +++ b/homeassistant/components/climate/hive.py @@ -1,178 +1,178 @@ -""" -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, - SUPPORT_AUX_HEAT, SUPPORT_TARGET_TEMPERATURE, SUPPORT_OPERATION_MODE) -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'} - -SUPPORT_FLAGS = (SUPPORT_TARGET_TEMPERATURE | - SUPPORT_OPERATION_MODE | - SUPPORT_AUX_HEAT) - - -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) - - @property - def supported_features(self): - """Return the list of supported features.""" - return SUPPORT_FLAGS - - 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) - - @property - def is_aux_heat_on(self): - """Return true if auxiliary heater is on.""" - boost_status = None - if self.device_type == "Heating": - boost_status = self.session.heating.get_boost(self.node_id) - elif self.device_type == "HotWater": - boost_status = self.session.hotwater.get_boost(self.node_id) - return boost_status == "ON" - - def turn_aux_heat_on(self): - """Turn auxiliary heater on.""" - target_boost_time = 30 - if self.device_type == "Heating": - curtemp = self.session.heating.current_temperature(self.node_id) - curtemp = round(curtemp * 2) / 2 - target_boost_temperature = curtemp + 0.5 - self.session.heating.turn_boost_on(self.node_id, - target_boost_time, - target_boost_temperature) - elif self.device_type == "HotWater": - self.session.hotwater.turn_boost_on(self.node_id, - target_boost_time) - - for entity in self.session.entities: - entity.handle_update(self.data_updatesource) - - def turn_aux_heat_off(self): - """Turn auxiliary heater off.""" - if self.device_type == "Heating": - self.session.heating.turn_boost_off(self.node_id) - elif self.device_type == "HotWater": - self.session.hotwater.turn_boost_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) +""" +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, + SUPPORT_AUX_HEAT, SUPPORT_TARGET_TEMPERATURE, SUPPORT_OPERATION_MODE) +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'} + +SUPPORT_FLAGS = (SUPPORT_TARGET_TEMPERATURE | + SUPPORT_OPERATION_MODE | + SUPPORT_AUX_HEAT) + + +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) + + @property + def supported_features(self): + """Return the list of supported features.""" + return SUPPORT_FLAGS + + 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) + + @property + def is_aux_heat_on(self): + """Return true if auxiliary heater is on.""" + boost_status = None + if self.device_type == "Heating": + boost_status = self.session.heating.get_boost(self.node_id) + elif self.device_type == "HotWater": + boost_status = self.session.hotwater.get_boost(self.node_id) + return boost_status == "ON" + + def turn_aux_heat_on(self): + """Turn auxiliary heater on.""" + target_boost_time = 30 + if self.device_type == "Heating": + curtemp = self.session.heating.current_temperature(self.node_id) + curtemp = round(curtemp * 2) / 2 + target_boost_temperature = curtemp + 0.5 + self.session.heating.turn_boost_on(self.node_id, + target_boost_time, + target_boost_temperature) + elif self.device_type == "HotWater": + self.session.hotwater.turn_boost_on(self.node_id, + target_boost_time) + + for entity in self.session.entities: + entity.handle_update(self.data_updatesource) + + def turn_aux_heat_off(self): + """Turn auxiliary heater off.""" + if self.device_type == "Heating": + self.session.heating.turn_boost_off(self.node_id) + elif self.device_type == "HotWater": + self.session.hotwater.turn_boost_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/homeassistant/components/hive.py b/homeassistant/components/hive.py index bf5196d6582..666854bdd8e 100644 --- a/homeassistant/components/hive.py +++ b/homeassistant/components/hive.py @@ -1,80 +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.10'] - -_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 +""" +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.10'] + +_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 index 3356d637be8..8fafb88a7db 100644 --- a/homeassistant/components/light/hive.py +++ b/homeassistant/components/light/hive.py @@ -1,141 +1,141 @@ -""" -Support for the Hive devices. - -For more details about this platform, please refer to the documentation at -https://home-assistant.io/components/light.hive/ -""" -import colorsys -from homeassistant.components.hive import DATA_HIVE -from homeassistant.components.light import (ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, - ATTR_RGB_COLOR, - 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 brightness(self): - """Brightness of the light (an integer in the range 1-255).""" - return self.session.light.get_brightness(self.node_id) - - @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_color_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_color_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 rgb_color(self) -> tuple: - """Return the RBG color value.""" - if self.light_device_type == "colourtuneablelight": - return self.session.light.get_color(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 - new_color = 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 ATTR_RGB_COLOR in kwargs: - get_new_color = kwargs.get(ATTR_RGB_COLOR) - tmp_new_color = colorsys.rgb_to_hsv(get_new_color[0], - get_new_color[1], - get_new_color[2]) - hue = int(round(tmp_new_color[0] * 360)) - saturation = int(round(tmp_new_color[1] * 100)) - value = int(round((tmp_new_color[2] / 255) * 100)) - new_color = (hue, saturation, value) - - self.session.light.turn_on(self.node_id, self.light_device_type, - new_brightness, new_color_temp, - new_color) - - 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) +""" +Support for the Hive devices. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/light.hive/ +""" +import colorsys +from homeassistant.components.hive import DATA_HIVE +from homeassistant.components.light import (ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, + ATTR_RGB_COLOR, + 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 brightness(self): + """Brightness of the light (an integer in the range 1-255).""" + return self.session.light.get_brightness(self.node_id) + + @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_color_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_color_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 rgb_color(self) -> tuple: + """Return the RBG color value.""" + if self.light_device_type == "colourtuneablelight": + return self.session.light.get_color(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 + new_color = 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 ATTR_RGB_COLOR in kwargs: + get_new_color = kwargs.get(ATTR_RGB_COLOR) + tmp_new_color = colorsys.rgb_to_hsv(get_new_color[0], + get_new_color[1], + get_new_color[2]) + hue = int(round(tmp_new_color[0] * 360)) + saturation = int(round(tmp_new_color[1] * 100)) + value = int(round((tmp_new_color[2] / 255) * 100)) + new_color = (hue, saturation, value) + + self.session.light.turn_on(self.node_id, self.light_device_type, + new_brightness, new_color_temp, + new_color) + + 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/map.py b/homeassistant/components/map.py index 2b204e584c3..b8293f64fc0 100644 --- a/homeassistant/components/map.py +++ b/homeassistant/components/map.py @@ -1,17 +1,17 @@ -""" -Provides a map panel for showing device locations. - -For more details about this component, please refer to the documentation at -https://home-assistant.io/components/map/ -""" -import asyncio - -DOMAIN = 'map' - - -@asyncio.coroutine -def async_setup(hass, config): - """Register the built-in map panel.""" - yield from hass.components.frontend.async_register_built_in_panel( - 'map', 'map', 'mdi:account-location') - return True +""" +Provides a map panel for showing device locations. + +For more details about this component, please refer to the documentation at +https://home-assistant.io/components/map/ +""" +import asyncio + +DOMAIN = 'map' + + +@asyncio.coroutine +def async_setup(hass, config): + """Register the built-in map panel.""" + yield from hass.components.frontend.async_register_built_in_panel( + 'map', 'map', 'mdi:account-location') + return True diff --git a/homeassistant/components/notify/prowl.py b/homeassistant/components/notify/prowl.py index 1298657a69a..3928fa81167 100644 --- a/homeassistant/components/notify/prowl.py +++ b/homeassistant/components/notify/prowl.py @@ -1,70 +1,70 @@ -""" -Prowl notification service. - -For more details about this platform, please refer to the documentation at -https://home-assistant.io/components/notify.prowl/ -""" -import logging -import asyncio - -import async_timeout -import voluptuous as vol - -from homeassistant.components.notify import ( - ATTR_TITLE, ATTR_TITLE_DEFAULT, ATTR_DATA, PLATFORM_SCHEMA, - BaseNotificationService) -from homeassistant.const import CONF_API_KEY -import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.aiohttp_client import async_get_clientsession - -_LOGGER = logging.getLogger(__name__) -_RESOURCE = 'https://api.prowlapp.com/publicapi/' - -PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ - vol.Required(CONF_API_KEY): cv.string, -}) - - -@asyncio.coroutine -def async_get_service(hass, config, discovery_info=None): - """Get the Prowl notification service.""" - return ProwlNotificationService(hass, config[CONF_API_KEY]) - - -class ProwlNotificationService(BaseNotificationService): - """Implement the notification service for Prowl.""" - - def __init__(self, hass, api_key): - """Initialize the service.""" - self._hass = hass - self._api_key = api_key - - @asyncio.coroutine - def async_send_message(self, message, **kwargs): - """Send the message to the user.""" - response = None - session = None - url = '{}{}'.format(_RESOURCE, 'add') - data = kwargs.get(ATTR_DATA) - payload = { - 'apikey': self._api_key, - 'application': 'Home-Assistant', - 'event': kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT), - 'description': message, - 'priority': data['priority'] if data and 'priority' in data else 0 - } - - _LOGGER.debug("Attempting call Prowl service at %s", url) - session = async_get_clientsession(self._hass) - - try: - with async_timeout.timeout(10, loop=self._hass.loop): - response = yield from session.post(url, data=payload) - result = yield from response.text() - - if response.status != 200 or 'error' in result: - _LOGGER.error("Prowl service returned http " - "status %d, response %s", - response.status, result) - except asyncio.TimeoutError: - _LOGGER.error("Timeout accessing Prowl at %s", url) +""" +Prowl notification service. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/notify.prowl/ +""" +import logging +import asyncio + +import async_timeout +import voluptuous as vol + +from homeassistant.components.notify import ( + ATTR_TITLE, ATTR_TITLE_DEFAULT, ATTR_DATA, PLATFORM_SCHEMA, + BaseNotificationService) +from homeassistant.const import CONF_API_KEY +import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.aiohttp_client import async_get_clientsession + +_LOGGER = logging.getLogger(__name__) +_RESOURCE = 'https://api.prowlapp.com/publicapi/' + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_API_KEY): cv.string, +}) + + +@asyncio.coroutine +def async_get_service(hass, config, discovery_info=None): + """Get the Prowl notification service.""" + return ProwlNotificationService(hass, config[CONF_API_KEY]) + + +class ProwlNotificationService(BaseNotificationService): + """Implement the notification service for Prowl.""" + + def __init__(self, hass, api_key): + """Initialize the service.""" + self._hass = hass + self._api_key = api_key + + @asyncio.coroutine + def async_send_message(self, message, **kwargs): + """Send the message to the user.""" + response = None + session = None + url = '{}{}'.format(_RESOURCE, 'add') + data = kwargs.get(ATTR_DATA) + payload = { + 'apikey': self._api_key, + 'application': 'Home-Assistant', + 'event': kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT), + 'description': message, + 'priority': data['priority'] if data and 'priority' in data else 0 + } + + _LOGGER.debug("Attempting call Prowl service at %s", url) + session = async_get_clientsession(self._hass) + + try: + with async_timeout.timeout(10, loop=self._hass.loop): + response = yield from session.post(url, data=payload) + result = yield from response.text() + + if response.status != 200 or 'error' in result: + _LOGGER.error("Prowl service returned http " + "status %d, response %s", + response.status, result) + except asyncio.TimeoutError: + _LOGGER.error("Timeout accessing Prowl at %s", url) diff --git a/homeassistant/components/sensor/hive.py b/homeassistant/components/sensor/hive.py index ce07dfdda5a..af31c14789a 100644 --- a/homeassistant/components/sensor/hive.py +++ b/homeassistant/components/sensor/hive.py @@ -1,52 +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) +""" +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 index d77247a5c04..97d4320280d 100644 --- a/homeassistant/components/switch/hive.py +++ b/homeassistant/components/switch/hive.py @@ -1,69 +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) +""" +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)