Minor style changes, cleanup (#13654)

* Minor style changes, cleanup
* Change 'self._entity.id' to 'self.entity_id'
* Use const 'STATE_OFF'
* Added CATEGORY constants
* Removed *args from accessory types
* Changed 'self._hass' to 'self.hass'
* Added log debug msg (for added lights)
This commit is contained in:
cdce8p 2018-04-05 00:52:25 +02:00 committed by GitHub
parent f263a931f7
commit 692b2644c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 87 additions and 87 deletions

View file

@ -102,8 +102,7 @@ def get_accessory(hass, state, aid, config):
aid=aid) aid=aid)
elif state.domain == 'alarm_control_panel': elif state.domain == 'alarm_control_panel':
_LOGGER.debug('Add "%s" as "%s"', state.entity_id, _LOGGER.debug('Add "%s" as "%s"', state.entity_id, 'SecuritySystem')
'SecuritySystem')
return TYPES['SecuritySystem'](hass, state.entity_id, state.name, return TYPES['SecuritySystem'](hass, state.entity_id, state.name,
alarm_code=config.get(ATTR_CODE), alarm_code=config.get(ATTR_CODE),
aid=aid) aid=aid)
@ -120,6 +119,7 @@ def get_accessory(hass, state, aid, config):
state.name, support_auto, aid=aid) state.name, support_auto, aid=aid)
elif state.domain == 'light': elif state.domain == 'light':
_LOGGER.debug('Add "%s" as "%s"', state.entity_id, 'Light')
return TYPES['Light'](hass, state.entity_id, state.name, aid=aid) return TYPES['Light'](hass, state.entity_id, state.name, aid=aid)
elif state.domain == 'switch' or state.domain == 'remote' \ elif state.domain == 'switch' or state.domain == 'remote' \

View file

@ -65,10 +65,10 @@ class HomeAccessory(Accessory):
def run(self): def run(self):
"""Method called by accessory after driver is started.""" """Method called by accessory after driver is started."""
state = self._hass.states.get(self._entity_id) state = self.hass.states.get(self.entity_id)
self.update_state(new_state=state) self.update_state(new_state=state)
async_track_state_change( async_track_state_change(
self._hass, self._entity_id, self.update_state) self.hass, self.entity_id, self.update_state)
class HomeBridge(Bridge): class HomeBridge(Bridge):
@ -79,7 +79,7 @@ class HomeBridge(Bridge):
"""Initialize a Bridge object.""" """Initialize a Bridge object."""
super().__init__(name, **kwargs) super().__init__(name, **kwargs)
set_accessory_info(self, name, model) set_accessory_info(self, name, model)
self._hass = hass self.hass = hass
def _set_services(self): def _set_services(self):
add_preload_service(self, SERV_ACCESSORY_INFO) add_preload_service(self, SERV_ACCESSORY_INFO)
@ -92,12 +92,12 @@ class HomeBridge(Bridge):
def add_paired_client(self, client_uuid, client_public): def add_paired_client(self, client_uuid, client_public):
"""Override super function to dismiss setup message if paired.""" """Override super function to dismiss setup message if paired."""
super().add_paired_client(client_uuid, client_public) super().add_paired_client(client_uuid, client_public)
dismiss_setup_message(self._hass) dismiss_setup_message(self.hass)
def remove_paired_client(self, client_uuid): def remove_paired_client(self, client_uuid):
"""Override super function to show setup message if unpaired.""" """Override super function to show setup message if unpaired."""
super().remove_paired_client(client_uuid) super().remove_paired_client(client_uuid)
show_setup_message(self, self._hass) show_setup_message(self, self.hass)
class HomeDriver(AccessoryDriver): class HomeDriver(AccessoryDriver):

View file

@ -24,8 +24,12 @@ BRIDGE_NAME = 'Home Assistant'
MANUFACTURER = 'HomeAssistant' MANUFACTURER = 'HomeAssistant'
# #### Categories #### # #### Categories ####
CATEGORY_ALARM_SYSTEM = 'ALARM_SYSTEM'
CATEGORY_LIGHT = 'LIGHTBULB' CATEGORY_LIGHT = 'LIGHTBULB'
CATEGORY_SENSOR = 'SENSOR' CATEGORY_SENSOR = 'SENSOR'
CATEGORY_SWITCH = 'SWITCH'
CATEGORY_THERMOSTAT = 'THERMOSTAT'
CATEGORY_WINDOW_COVERING = 'WINDOW_COVERING'
# #### Services #### # #### Services ####

View file

@ -6,8 +6,8 @@ from homeassistant.components.cover import ATTR_CURRENT_POSITION
from . import TYPES from . import TYPES
from .accessories import HomeAccessory, add_preload_service from .accessories import HomeAccessory, add_preload_service
from .const import ( from .const import (
SERV_WINDOW_COVERING, CHAR_CURRENT_POSITION, CATEGORY_WINDOW_COVERING, SERV_WINDOW_COVERING,
CHAR_TARGET_POSITION, CHAR_POSITION_STATE) CHAR_CURRENT_POSITION, CHAR_TARGET_POSITION, CHAR_POSITION_STATE)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -20,13 +20,13 @@ class WindowCovering(HomeAccessory):
The cover entity must support: set_cover_position. The cover entity must support: set_cover_position.
""" """
def __init__(self, hass, entity_id, display_name, *args, **kwargs): def __init__(self, hass, entity_id, display_name, **kwargs):
"""Initialize a WindowCovering accessory object.""" """Initialize a WindowCovering accessory object."""
super().__init__(display_name, entity_id, 'WINDOW_COVERING', super().__init__(display_name, entity_id,
*args, **kwargs) CATEGORY_WINDOW_COVERING, **kwargs)
self._hass = hass self.hass = hass
self._entity_id = entity_id self.entity_id = entity_id
self.current_position = None self.current_position = None
self.homekit_target = None self.homekit_target = None
@ -48,14 +48,14 @@ class WindowCovering(HomeAccessory):
"""Move cover to value if call came from HomeKit.""" """Move cover to value if call came from HomeKit."""
self.char_target_position.set_value(value, should_callback=False) self.char_target_position.set_value(value, should_callback=False)
if value != self.current_position: if value != self.current_position:
_LOGGER.debug('%s: Set position to %d', self._entity_id, value) _LOGGER.debug('%s: Set position to %d', self.entity_id, value)
self.homekit_target = value self.homekit_target = value
if value > self.current_position: if value > self.current_position:
self.char_position_state.set_value(1) self.char_position_state.set_value(1)
elif value < self.current_position: elif value < self.current_position:
self.char_position_state.set_value(0) self.char_position_state.set_value(0)
self._hass.components.cover.set_cover_position( self.hass.components.cover.set_cover_position(
value, self._entity_id) value, self.entity_id)
def update_state(self, entity_id=None, old_state=None, new_state=None): def update_state(self, entity_id=None, old_state=None, new_state=None):
"""Update cover position after state changed.""" """Update cover position after state changed."""

View file

@ -23,19 +23,19 @@ class Light(HomeAccessory):
Currently supports: state, brightness, rgb_color. Currently supports: state, brightness, rgb_color.
""" """
def __init__(self, hass, entity_id, name, *args, **kwargs): def __init__(self, hass, entity_id, name, **kwargs):
"""Initialize a new Light accessory object.""" """Initialize a new Light accessory object."""
super().__init__(name, entity_id, CATEGORY_LIGHT, *args, **kwargs) super().__init__(name, entity_id, CATEGORY_LIGHT, **kwargs)
self._hass = hass self.hass = hass
self._entity_id = entity_id self.entity_id = entity_id
self._flag = {CHAR_ON: False, CHAR_BRIGHTNESS: False, self._flag = {CHAR_ON: False, CHAR_BRIGHTNESS: False,
CHAR_HUE: False, CHAR_SATURATION: False, CHAR_HUE: False, CHAR_SATURATION: False,
RGB_COLOR: False} RGB_COLOR: False}
self._state = 0 self._state = 0
self.chars = [] self.chars = []
self._features = self._hass.states.get(self._entity_id) \ self._features = self.hass.states.get(self.entity_id) \
.attributes.get(ATTR_SUPPORTED_FEATURES) .attributes.get(ATTR_SUPPORTED_FEATURES)
if self._features & SUPPORT_BRIGHTNESS: if self._features & SUPPORT_BRIGHTNESS:
self.chars.append(CHAR_BRIGHTNESS) self.chars.append(CHAR_BRIGHTNESS)
@ -70,29 +70,29 @@ class Light(HomeAccessory):
if self._state == value: if self._state == value:
return return
_LOGGER.debug('%s: Set state to %d', self._entity_id, value) _LOGGER.debug('%s: Set state to %d', self.entity_id, value)
self._flag[CHAR_ON] = True self._flag[CHAR_ON] = True
self.char_on.set_value(value, should_callback=False) self.char_on.set_value(value, should_callback=False)
if value == 1: if value == 1:
self._hass.components.light.turn_on(self._entity_id) self.hass.components.light.turn_on(self.entity_id)
elif value == 0: elif value == 0:
self._hass.components.light.turn_off(self._entity_id) self.hass.components.light.turn_off(self.entity_id)
def set_brightness(self, value): def set_brightness(self, value):
"""Set brightness if call came from HomeKit.""" """Set brightness if call came from HomeKit."""
_LOGGER.debug('%s: Set brightness to %d', self._entity_id, value) _LOGGER.debug('%s: Set brightness to %d', self.entity_id, value)
self._flag[CHAR_BRIGHTNESS] = True self._flag[CHAR_BRIGHTNESS] = True
self.char_brightness.set_value(value, should_callback=False) self.char_brightness.set_value(value, should_callback=False)
if value != 0: if value != 0:
self._hass.components.light.turn_on( self.hass.components.light.turn_on(
self._entity_id, brightness_pct=value) self.entity_id, brightness_pct=value)
else: else:
self._hass.components.light.turn_off(self._entity_id) self.hass.components.light.turn_off(self.entity_id)
def set_saturation(self, value): def set_saturation(self, value):
"""Set saturation if call came from HomeKit.""" """Set saturation if call came from HomeKit."""
_LOGGER.debug('%s: Set saturation to %d', self._entity_id, value) _LOGGER.debug('%s: Set saturation to %d', self.entity_id, value)
self._flag[CHAR_SATURATION] = True self._flag[CHAR_SATURATION] = True
self.char_saturation.set_value(value, should_callback=False) self.char_saturation.set_value(value, should_callback=False)
self._saturation = value self._saturation = value
@ -100,7 +100,7 @@ class Light(HomeAccessory):
def set_hue(self, value): def set_hue(self, value):
"""Set hue if call came from HomeKit.""" """Set hue if call came from HomeKit."""
_LOGGER.debug('%s: Set hue to %d', self._entity_id, value) _LOGGER.debug('%s: Set hue to %d', self.entity_id, value)
self._flag[CHAR_HUE] = True self._flag[CHAR_HUE] = True
self.char_hue.set_value(value, should_callback=False) self.char_hue.set_value(value, should_callback=False)
self._hue = value self._hue = value
@ -112,11 +112,11 @@ class Light(HomeAccessory):
if self._features & SUPPORT_COLOR and self._flag[CHAR_HUE] and \ if self._features & SUPPORT_COLOR and self._flag[CHAR_HUE] and \
self._flag[CHAR_SATURATION]: self._flag[CHAR_SATURATION]:
color = (self._hue, self._saturation) color = (self._hue, self._saturation)
_LOGGER.debug('%s: Set hs_color to %s', self._entity_id, color) _LOGGER.debug('%s: Set hs_color to %s', self.entity_id, color)
self._flag.update({ self._flag.update({
CHAR_HUE: False, CHAR_SATURATION: False, RGB_COLOR: True}) CHAR_HUE: False, CHAR_SATURATION: False, RGB_COLOR: True})
self._hass.components.light.turn_on( self.hass.components.light.turn_on(
self._entity_id, hs_color=color) self.entity_id, hs_color=color)
def update_state(self, entity_id=None, old_state=None, new_state=None): def update_state(self, entity_id=None, old_state=None, new_state=None):
"""Update light after state change.""" """Update light after state change."""

View file

@ -9,8 +9,8 @@ from homeassistant.const import (
from . import TYPES from . import TYPES
from .accessories import HomeAccessory, add_preload_service from .accessories import HomeAccessory, add_preload_service
from .const import ( from .const import (
SERV_SECURITY_SYSTEM, CHAR_CURRENT_SECURITY_STATE, CATEGORY_ALARM_SYSTEM, SERV_SECURITY_SYSTEM,
CHAR_TARGET_SECURITY_STATE) CHAR_CURRENT_SECURITY_STATE, CHAR_TARGET_SECURITY_STATE)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -27,14 +27,13 @@ STATE_TO_SERVICE = {STATE_ALARM_DISARMED: 'alarm_disarm',
class SecuritySystem(HomeAccessory): class SecuritySystem(HomeAccessory):
"""Generate an SecuritySystem accessory for an alarm control panel.""" """Generate an SecuritySystem accessory for an alarm control panel."""
def __init__(self, hass, entity_id, display_name, def __init__(self, hass, entity_id, display_name, alarm_code, **kwargs):
alarm_code, *args, **kwargs):
"""Initialize a SecuritySystem accessory object.""" """Initialize a SecuritySystem accessory object."""
super().__init__(display_name, entity_id, 'ALARM_SYSTEM', super().__init__(display_name, entity_id,
*args, **kwargs) CATEGORY_ALARM_SYSTEM, **kwargs)
self._hass = hass self.hass = hass
self._entity_id = entity_id self.entity_id = entity_id
self._alarm_code = alarm_code self._alarm_code = alarm_code
self.flag_target_state = False self.flag_target_state = False
@ -52,16 +51,16 @@ class SecuritySystem(HomeAccessory):
def set_security_state(self, value): def set_security_state(self, value):
"""Move security state to value if call came from HomeKit.""" """Move security state to value if call came from HomeKit."""
_LOGGER.debug('%s: Set security state to %d', _LOGGER.debug('%s: Set security state to %d',
self._entity_id, value) self.entity_id, value)
self.flag_target_state = True self.flag_target_state = True
self.char_target_state.set_value(value, should_callback=False) self.char_target_state.set_value(value, should_callback=False)
hass_value = HOMEKIT_TO_HASS[value] hass_value = HOMEKIT_TO_HASS[value]
service = STATE_TO_SERVICE[hass_value] service = STATE_TO_SERVICE[hass_value]
params = {ATTR_ENTITY_ID: self._entity_id} params = {ATTR_ENTITY_ID: self.entity_id}
if self._alarm_code: if self._alarm_code:
params[ATTR_CODE] = self._alarm_code params[ATTR_CODE] = self._alarm_code
self._hass.services.call('alarm_control_panel', service, params) self.hass.services.call('alarm_control_panel', service, params)
def update_state(self, entity_id=None, old_state=None, new_state=None): def update_state(self, entity_id=None, old_state=None, new_state=None):
"""Update security state after state changed.""" """Update security state after state changed."""
@ -76,7 +75,7 @@ class SecuritySystem(HomeAccessory):
self.char_current_state.set_value(current_security_state, self.char_current_state.set_value(current_security_state,
should_callback=False) should_callback=False)
_LOGGER.debug('%s: Updated current state to %s (%d)', _LOGGER.debug('%s: Updated current state to %s (%d)',
self._entity_id, hass_state, current_security_state) self.entity_id, hass_state, current_security_state)
if not self.flag_target_state: if not self.flag_target_state:
self.char_target_state.set_value(current_security_state, self.char_target_state.set_value(current_security_state,

View file

@ -23,12 +23,12 @@ class TemperatureSensor(HomeAccessory):
Sensor entity must return temperature in °C, °F. Sensor entity must return temperature in °C, °F.
""" """
def __init__(self, hass, entity_id, name, *args, **kwargs): def __init__(self, hass, entity_id, name, **kwargs):
"""Initialize a TemperatureSensor accessory object.""" """Initialize a TemperatureSensor accessory object."""
super().__init__(name, entity_id, CATEGORY_SENSOR, *args, **kwargs) super().__init__(name, entity_id, CATEGORY_SENSOR, **kwargs)
self._hass = hass self.hass = hass
self._entity_id = entity_id self.entity_id = entity_id
serv_temp = add_preload_service(self, SERV_TEMPERATURE_SENSOR) serv_temp = add_preload_service(self, SERV_TEMPERATURE_SENSOR)
self.char_temp = serv_temp.get_characteristic(CHAR_CURRENT_TEMPERATURE) self.char_temp = serv_temp.get_characteristic(CHAR_CURRENT_TEMPERATURE)
@ -47,7 +47,7 @@ class TemperatureSensor(HomeAccessory):
temperature = temperature_to_homekit(temperature, unit) temperature = temperature_to_homekit(temperature, unit)
self.char_temp.set_value(temperature, should_callback=False) self.char_temp.set_value(temperature, should_callback=False)
_LOGGER.debug('%s: Current temperature set to %d°C', _LOGGER.debug('%s: Current temperature set to %d°C',
self._entity_id, temperature) self.entity_id, temperature)
@TYPES.register('HumiditySensor') @TYPES.register('HumiditySensor')
@ -58,8 +58,8 @@ class HumiditySensor(HomeAccessory):
"""Initialize a HumiditySensor accessory object.""" """Initialize a HumiditySensor accessory object."""
super().__init__(name, entity_id, CATEGORY_SENSOR, *args, **kwargs) super().__init__(name, entity_id, CATEGORY_SENSOR, *args, **kwargs)
self._hass = hass self.hass = hass
self._entity_id = entity_id self.entity_id = entity_id
serv_humidity = add_preload_service(self, SERV_HUMIDITY_SENSOR) serv_humidity = add_preload_service(self, SERV_HUMIDITY_SENSOR)
self.char_humidity = serv_humidity \ self.char_humidity = serv_humidity \
@ -75,4 +75,4 @@ class HumiditySensor(HomeAccessory):
if humidity: if humidity:
self.char_humidity.set_value(humidity, should_callback=False) self.char_humidity.set_value(humidity, should_callback=False)
_LOGGER.debug('%s: Percent set to %d%%', _LOGGER.debug('%s: Percent set to %d%%',
self._entity_id, humidity) self.entity_id, humidity)

View file

@ -7,7 +7,7 @@ from homeassistant.core import split_entity_id
from . import TYPES from . import TYPES
from .accessories import HomeAccessory, add_preload_service from .accessories import HomeAccessory, add_preload_service
from .const import SERV_SWITCH, CHAR_ON from .const import CATEGORY_SWITCH, SERV_SWITCH, CHAR_ON
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -16,12 +16,12 @@ _LOGGER = logging.getLogger(__name__)
class Switch(HomeAccessory): class Switch(HomeAccessory):
"""Generate a Switch accessory.""" """Generate a Switch accessory."""
def __init__(self, hass, entity_id, display_name, *args, **kwargs): def __init__(self, hass, entity_id, display_name, **kwargs):
"""Initialize a Switch accessory object to represent a remote.""" """Initialize a Switch accessory object to represent a remote."""
super().__init__(display_name, entity_id, 'SWITCH', *args, **kwargs) super().__init__(display_name, entity_id, CATEGORY_SWITCH, **kwargs)
self._hass = hass self.hass = hass
self._entity_id = entity_id self.entity_id = entity_id
self._domain = split_entity_id(entity_id)[0] self._domain = split_entity_id(entity_id)[0]
self.flag_target_state = False self.flag_target_state = False
@ -34,12 +34,12 @@ class Switch(HomeAccessory):
def set_state(self, value): def set_state(self, value):
"""Move switch state to value if call came from HomeKit.""" """Move switch state to value if call came from HomeKit."""
_LOGGER.debug('%s: Set switch state to %s', _LOGGER.debug('%s: Set switch state to %s',
self._entity_id, value) self.entity_id, value)
self.flag_target_state = True self.flag_target_state = True
self.char_on.set_value(value, should_callback=False) self.char_on.set_value(value, should_callback=False)
service = SERVICE_TURN_ON if value else SERVICE_TURN_OFF service = SERVICE_TURN_ON if value else SERVICE_TURN_OFF
self._hass.services.call(self._domain, service, self.hass.services.call(self._domain, service,
{ATTR_ENTITY_ID: self._entity_id}) {ATTR_ENTITY_ID: self.entity_id})
def update_state(self, entity_id=None, old_state=None, new_state=None): def update_state(self, entity_id=None, old_state=None, new_state=None):
"""Update switch state after state changed.""" """Update switch state after state changed."""
@ -49,7 +49,7 @@ class Switch(HomeAccessory):
current_state = (new_state.state == STATE_ON) current_state = (new_state.state == STATE_ON)
if not self.flag_target_state: if not self.flag_target_state:
_LOGGER.debug('%s: Set current state to %s', _LOGGER.debug('%s: Set current state to %s',
self._entity_id, current_state) self.entity_id, current_state)
self.char_on.set_value(current_state, should_callback=False) self.char_on.set_value(current_state, should_callback=False)
self.flag_target_state = False self.flag_target_state = False

View file

@ -7,12 +7,12 @@ from homeassistant.components.climate import (
ATTR_OPERATION_MODE, ATTR_OPERATION_LIST, ATTR_OPERATION_MODE, ATTR_OPERATION_LIST,
STATE_HEAT, STATE_COOL, STATE_AUTO) STATE_HEAT, STATE_COOL, STATE_AUTO)
from homeassistant.const import ( from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS, TEMP_FAHRENHEIT) ATTR_UNIT_OF_MEASUREMENT, STATE_OFF, TEMP_CELSIUS, TEMP_FAHRENHEIT)
from . import TYPES from . import TYPES
from .accessories import HomeAccessory, add_preload_service from .accessories import HomeAccessory, add_preload_service
from .const import ( from .const import (
SERV_THERMOSTAT, CHAR_CURRENT_HEATING_COOLING, CATEGORY_THERMOSTAT, SERV_THERMOSTAT, CHAR_CURRENT_HEATING_COOLING,
CHAR_TARGET_HEATING_COOLING, CHAR_CURRENT_TEMPERATURE, CHAR_TARGET_HEATING_COOLING, CHAR_CURRENT_TEMPERATURE,
CHAR_TARGET_TEMPERATURE, CHAR_TEMP_DISPLAY_UNITS, CHAR_TARGET_TEMPERATURE, CHAR_TEMP_DISPLAY_UNITS,
CHAR_COOLING_THRESHOLD_TEMPERATURE, CHAR_HEATING_THRESHOLD_TEMPERATURE) CHAR_COOLING_THRESHOLD_TEMPERATURE, CHAR_HEATING_THRESHOLD_TEMPERATURE)
@ -20,7 +20,6 @@ from .util import temperature_to_homekit, temperature_to_states
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
STATE_OFF = 'off'
UNIT_HASS_TO_HOMEKIT = {TEMP_CELSIUS: 0, TEMP_FAHRENHEIT: 1} UNIT_HASS_TO_HOMEKIT = {TEMP_CELSIUS: 0, TEMP_FAHRENHEIT: 1}
UNIT_HOMEKIT_TO_HASS = {c: s for s, c in UNIT_HASS_TO_HOMEKIT.items()} UNIT_HOMEKIT_TO_HASS = {c: s for s, c in UNIT_HASS_TO_HOMEKIT.items()}
HC_HASS_TO_HOMEKIT = {STATE_OFF: 0, STATE_HEAT: 1, HC_HASS_TO_HOMEKIT = {STATE_OFF: 0, STATE_HEAT: 1,
@ -32,14 +31,13 @@ HC_HOMEKIT_TO_HASS = {c: s for s, c in HC_HASS_TO_HOMEKIT.items()}
class Thermostat(HomeAccessory): class Thermostat(HomeAccessory):
"""Generate a Thermostat accessory for a climate.""" """Generate a Thermostat accessory for a climate."""
def __init__(self, hass, entity_id, display_name, def __init__(self, hass, entity_id, display_name, support_auto, **kwargs):
support_auto, *args, **kwargs):
"""Initialize a Thermostat accessory object.""" """Initialize a Thermostat accessory object."""
super().__init__(display_name, entity_id, 'THERMOSTAT', super().__init__(display_name, entity_id,
*args, **kwargs) CATEGORY_THERMOSTAT, **kwargs)
self._hass = hass self.hass = hass
self._entity_id = entity_id self.entity_id = entity_id
self._call_timer = None self._call_timer = None
self._unit = TEMP_CELSIUS self._unit = TEMP_CELSIUS
@ -101,48 +99,48 @@ class Thermostat(HomeAccessory):
"""Move operation mode to value if call came from HomeKit.""" """Move operation mode to value if call came from HomeKit."""
self.char_target_heat_cool.set_value(value, should_callback=False) self.char_target_heat_cool.set_value(value, should_callback=False)
if value in HC_HOMEKIT_TO_HASS: if value in HC_HOMEKIT_TO_HASS:
_LOGGER.debug('%s: Set heat-cool to %d', self._entity_id, value) _LOGGER.debug('%s: Set heat-cool to %d', self.entity_id, value)
self.heat_cool_flag_target_state = True self.heat_cool_flag_target_state = True
hass_value = HC_HOMEKIT_TO_HASS[value] hass_value = HC_HOMEKIT_TO_HASS[value]
self._hass.components.climate.set_operation_mode( self.hass.components.climate.set_operation_mode(
operation_mode=hass_value, entity_id=self._entity_id) operation_mode=hass_value, entity_id=self.entity_id)
def set_cooling_threshold(self, value): def set_cooling_threshold(self, value):
"""Set cooling threshold temp to value if call came from HomeKit.""" """Set cooling threshold temp to value if call came from HomeKit."""
_LOGGER.debug('%s: Set cooling threshold temperature to %.2f°C', _LOGGER.debug('%s: Set cooling threshold temperature to %.2f°C',
self._entity_id, value) self.entity_id, value)
self.coolingthresh_flag_target_state = True self.coolingthresh_flag_target_state = True
self.char_cooling_thresh_temp.set_value(value, should_callback=False) self.char_cooling_thresh_temp.set_value(value, should_callback=False)
low = self.char_heating_thresh_temp.value low = self.char_heating_thresh_temp.value
low = temperature_to_states(low, self._unit) low = temperature_to_states(low, self._unit)
value = temperature_to_states(value, self._unit) value = temperature_to_states(value, self._unit)
self._hass.components.climate.set_temperature( self.hass.components.climate.set_temperature(
entity_id=self._entity_id, target_temp_high=value, entity_id=self.entity_id, target_temp_high=value,
target_temp_low=low) target_temp_low=low)
def set_heating_threshold(self, value): def set_heating_threshold(self, value):
"""Set heating threshold temp to value if call came from HomeKit.""" """Set heating threshold temp to value if call came from HomeKit."""
_LOGGER.debug('%s: Set heating threshold temperature to %.2f°C', _LOGGER.debug('%s: Set heating threshold temperature to %.2f°C',
self._entity_id, value) self.entity_id, value)
self.heatingthresh_flag_target_state = True self.heatingthresh_flag_target_state = True
self.char_heating_thresh_temp.set_value(value, should_callback=False) self.char_heating_thresh_temp.set_value(value, should_callback=False)
# Home assistant always wants to set low and high at the same time # Home assistant always wants to set low and high at the same time
high = self.char_cooling_thresh_temp.value high = self.char_cooling_thresh_temp.value
high = temperature_to_states(high, self._unit) high = temperature_to_states(high, self._unit)
value = temperature_to_states(value, self._unit) value = temperature_to_states(value, self._unit)
self._hass.components.climate.set_temperature( self.hass.components.climate.set_temperature(
entity_id=self._entity_id, target_temp_high=high, entity_id=self.entity_id, target_temp_high=high,
target_temp_low=value) target_temp_low=value)
def set_target_temperature(self, value): def set_target_temperature(self, value):
"""Set target temperature to value if call came from HomeKit.""" """Set target temperature to value if call came from HomeKit."""
_LOGGER.debug('%s: Set target temperature to %.2f°C', _LOGGER.debug('%s: Set target temperature to %.2f°C',
self._entity_id, value) self.entity_id, value)
self.temperature_flag_target_state = True self.temperature_flag_target_state = True
self.char_target_temp.set_value(value, should_callback=False) self.char_target_temp.set_value(value, should_callback=False)
value = temperature_to_states(value, self._unit) value = temperature_to_states(value, self._unit)
self._hass.components.climate.set_temperature( self.hass.components.climate.set_temperature(
temperature=value, entity_id=self._entity_id) temperature=value, entity_id=self.entity_id)
def update_state(self, entity_id=None, old_state=None, new_state=None): def update_state(self, entity_id=None, old_state=None, new_state=None):
"""Update security state after state changed.""" """Update security state after state changed."""

View file

@ -6,11 +6,10 @@ from homeassistant.components.climate import (
ATTR_CURRENT_TEMPERATURE, ATTR_TEMPERATURE, ATTR_CURRENT_TEMPERATURE, ATTR_TEMPERATURE,
ATTR_TARGET_TEMP_LOW, ATTR_TARGET_TEMP_HIGH, ATTR_OPERATION_MODE, ATTR_TARGET_TEMP_LOW, ATTR_TARGET_TEMP_HIGH, ATTR_OPERATION_MODE,
ATTR_OPERATION_LIST, STATE_COOL, STATE_HEAT, STATE_AUTO) ATTR_OPERATION_LIST, STATE_COOL, STATE_HEAT, STATE_AUTO)
from homeassistant.components.homekit.type_thermostats import ( from homeassistant.components.homekit.type_thermostats import Thermostat
Thermostat, STATE_OFF)
from homeassistant.const import ( from homeassistant.const import (
ATTR_SERVICE, EVENT_CALL_SERVICE, ATTR_SERVICE_DATA, ATTR_SERVICE, EVENT_CALL_SERVICE, ATTR_SERVICE_DATA,
ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS, TEMP_FAHRENHEIT) ATTR_UNIT_OF_MEASUREMENT, STATE_OFF, TEMP_CELSIUS, TEMP_FAHRENHEIT)
from tests.common import get_test_home_assistant from tests.common import get_test_home_assistant