Move homekit_controller entity types under homekit_controller platform (#20376)
* Move homekit_controller entity types under homekit_controller platform * Update coveragerc as homekit_controller moved
This commit is contained in:
1 parent
1fcaaf93ad
commit
cea2bf94bd
7 files changed
+1
-2
No files matched your search
@@ -0,0 +1,117 @@
|
||||
"""
|
||||
Support for Homekit Alarm Control Panel.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/alarm_control_panel.homekit_controller/
|
||||
"""
|
||||
import logging
|
||||
|
||||
from homeassistant.components.homekit_controller import (HomeKitEntity,
|
||||
KNOWN_ACCESSORIES)
|
||||
from homeassistant.components.alarm_control_panel import AlarmControlPanel
|
||||
from homeassistant.const import (
|
||||
STATE_ALARM_DISARMED, STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME,
|
||||
STATE_ALARM_ARMED_NIGHT, STATE_ALARM_TRIGGERED)
|
||||
from homeassistant.const import ATTR_BATTERY_LEVEL
|
||||
|
||||
DEPENDENCIES = ['homekit_controller']
|
||||
|
||||
ICON = 'mdi:security'
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CURRENT_STATE_MAP = {
|
||||
0: STATE_ALARM_ARMED_HOME,
|
||||
1: STATE_ALARM_ARMED_AWAY,
|
||||
2: STATE_ALARM_ARMED_NIGHT,
|
||||
3: STATE_ALARM_DISARMED,
|
||||
4: STATE_ALARM_TRIGGERED
|
||||
}
|
||||
|
||||
TARGET_STATE_MAP = {
|
||||
STATE_ALARM_ARMED_HOME: 0,
|
||||
STATE_ALARM_ARMED_AWAY: 1,
|
||||
STATE_ALARM_ARMED_NIGHT: 2,
|
||||
STATE_ALARM_DISARMED: 3,
|
||||
}
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up Homekit Alarm Control Panel support."""
|
||||
if discovery_info is None:
|
||||
return
|
||||
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
|
||||
add_entities([HomeKitAlarmControlPanel(accessory, discovery_info)],
|
||||
True)
|
||||
|
||||
|
||||
class HomeKitAlarmControlPanel(HomeKitEntity, AlarmControlPanel):
|
||||
"""Representation of a Homekit Alarm Control Panel."""
|
||||
|
||||
def __init__(self, *args):
|
||||
"""Initialise the Alarm Control Panel."""
|
||||
super().__init__(*args)
|
||||
self._state = None
|
||||
self._battery_level = None
|
||||
|
||||
def update_characteristics(self, characteristics):
|
||||
"""Synchronise the Alarm Control Panel state with Home Assistant."""
|
||||
# pylint: disable=import-error
|
||||
from homekit.model.characteristics import CharacteristicsTypes
|
||||
|
||||
for characteristic in characteristics:
|
||||
ctype = characteristic['type']
|
||||
ctype = CharacteristicsTypes.get_short(ctype)
|
||||
if ctype == "security-system-state.current":
|
||||
self._chars['security-system-state.current'] = \
|
||||
characteristic['iid']
|
||||
self._state = CURRENT_STATE_MAP[characteristic['value']]
|
||||
elif ctype == "security-system-state.target":
|
||||
self._chars['security-system-state.target'] = \
|
||||
characteristic['iid']
|
||||
elif ctype == "battery-level":
|
||||
self._chars['battery-level'] = characteristic['iid']
|
||||
self._battery_level = characteristic['value']
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return icon."""
|
||||
return ICON
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the device."""
|
||||
return self._state
|
||||
|
||||
def alarm_disarm(self, code=None):
|
||||
"""Send disarm command."""
|
||||
self.set_alarm_state(STATE_ALARM_DISARMED, code)
|
||||
|
||||
def alarm_arm_away(self, code=None):
|
||||
"""Send arm command."""
|
||||
self.set_alarm_state(STATE_ALARM_ARMED_AWAY, code)
|
||||
|
||||
def alarm_arm_home(self, code=None):
|
||||
"""Send stay command."""
|
||||
self.set_alarm_state(STATE_ALARM_ARMED_HOME, code)
|
||||
|
||||
def alarm_arm_night(self, code=None):
|
||||
"""Send night command."""
|
||||
self.set_alarm_state(STATE_ALARM_ARMED_NIGHT, code)
|
||||
|
||||
def set_alarm_state(self, state, code=None):
|
||||
"""Send state command."""
|
||||
characteristics = [{'aid': self._aid,
|
||||
'iid': self._chars['security-system-state.target'],
|
||||
'value': TARGET_STATE_MAP[state]}]
|
||||
self.put_characteristics(characteristics)
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the optional state attributes."""
|
||||
if self._battery_level is None:
|
||||
return None
|
||||
|
||||
return {
|
||||
ATTR_BATTERY_LEVEL: self._battery_level,
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
"""
|
||||
Support for Homekit climate devices.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/climate.homekit_controller/
|
||||
"""
|
||||
import logging
|
||||
|
||||
from homeassistant.components.homekit_controller import (
|
||||
HomeKitEntity, KNOWN_ACCESSORIES)
|
||||
from homeassistant.components.climate import (
|
||||
ClimateDevice, STATE_HEAT, STATE_COOL, STATE_IDLE,
|
||||
SUPPORT_TARGET_TEMPERATURE, SUPPORT_OPERATION_MODE)
|
||||
from homeassistant.const import TEMP_CELSIUS, STATE_OFF, ATTR_TEMPERATURE
|
||||
|
||||
DEPENDENCIES = ['homekit_controller']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# Map of Homekit operation modes to hass modes
|
||||
MODE_HOMEKIT_TO_HASS = {
|
||||
0: STATE_OFF,
|
||||
1: STATE_HEAT,
|
||||
2: STATE_COOL,
|
||||
}
|
||||
|
||||
# Map of hass operation modes to homekit modes
|
||||
MODE_HASS_TO_HOMEKIT = {v: k for k, v in MODE_HOMEKIT_TO_HASS.items()}
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up Homekit climate."""
|
||||
if discovery_info is not None:
|
||||
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
|
||||
add_entities([HomeKitClimateDevice(accessory, discovery_info)], True)
|
||||
|
||||
|
||||
class HomeKitClimateDevice(HomeKitEntity, ClimateDevice):
|
||||
"""Representation of a Homekit climate device."""
|
||||
|
||||
def __init__(self, *args):
|
||||
"""Initialise the device."""
|
||||
super().__init__(*args)
|
||||
self._state = None
|
||||
self._current_mode = None
|
||||
self._valid_modes = []
|
||||
self._current_temp = None
|
||||
self._target_temp = None
|
||||
|
||||
def update_characteristics(self, characteristics):
|
||||
"""Synchronise device state with Home Assistant."""
|
||||
# pylint: disable=import-error
|
||||
from homekit.models.characteristics import CharacteristicsTypes
|
||||
|
||||
for characteristic in characteristics:
|
||||
ctype = characteristic['type']
|
||||
if ctype == CharacteristicsTypes.HEATING_COOLING_CURRENT:
|
||||
self._state = MODE_HOMEKIT_TO_HASS.get(
|
||||
characteristic['value'])
|
||||
if ctype == CharacteristicsTypes.HEATING_COOLING_TARGET:
|
||||
self._chars['target_mode'] = characteristic['iid']
|
||||
self._features |= SUPPORT_OPERATION_MODE
|
||||
self._current_mode = MODE_HOMEKIT_TO_HASS.get(
|
||||
characteristic['value'])
|
||||
self._valid_modes = [MODE_HOMEKIT_TO_HASS.get(
|
||||
mode) for mode in characteristic['valid-values']]
|
||||
elif ctype == CharacteristicsTypes.TEMPERATURE_CURRENT:
|
||||
self._current_temp = characteristic['value']
|
||||
elif ctype == CharacteristicsTypes.TEMPERATURE_TARGET:
|
||||
self._chars['target_temp'] = characteristic['iid']
|
||||
self._features |= SUPPORT_TARGET_TEMPERATURE
|
||||
self._target_temp = characteristic['value']
|
||||
|
||||
def set_temperature(self, **kwargs):
|
||||
"""Set new target temperature."""
|
||||
temp = kwargs.get(ATTR_TEMPERATURE)
|
||||
|
||||
characteristics = [{'aid': self._aid,
|
||||
'iid': self._chars['target_temp'],
|
||||
'value': temp}]
|
||||
self.put_characteristics(characteristics)
|
||||
|
||||
def set_operation_mode(self, operation_mode):
|
||||
"""Set new target operation mode."""
|
||||
characteristics = [{'aid': self._aid,
|
||||
'iid': self._chars['target_mode'],
|
||||
'value': MODE_HASS_TO_HOMEKIT[operation_mode]}]
|
||||
self.put_characteristics(characteristics)
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the current state."""
|
||||
# If the device reports its operating mode as off, it sometimes doesn't
|
||||
# report a new state.
|
||||
if self._current_mode == STATE_OFF:
|
||||
return STATE_OFF
|
||||
|
||||
if self._state == STATE_OFF and self._current_mode != STATE_OFF:
|
||||
return STATE_IDLE
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def current_temperature(self):
|
||||
"""Return the current temperature."""
|
||||
return self._current_temp
|
||||
|
||||
@property
|
||||
def target_temperature(self):
|
||||
"""Return the temperature we try to reach."""
|
||||
return self._target_temp
|
||||
|
||||
@property
|
||||
def current_operation(self):
|
||||
"""Return current operation ie. heat, cool, idle."""
|
||||
return self._current_mode
|
||||
|
||||
@property
|
||||
def operation_list(self):
|
||||
"""Return the list of available operation modes."""
|
||||
return self._valid_modes
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
"""Return the list of supported features."""
|
||||
return self._features
|
||||
|
||||
@property
|
||||
def temperature_unit(self):
|
||||
"""Return the unit of measurement."""
|
||||
return TEMP_CELSIUS
|
||||
@@ -0,0 +1,305 @@
|
||||
"""
|
||||
Support for Homekit Cover.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/cover.homekit_controller/
|
||||
"""
|
||||
import logging
|
||||
|
||||
from homeassistant.components.homekit_controller import (HomeKitEntity,
|
||||
KNOWN_ACCESSORIES)
|
||||
from homeassistant.components.cover import (
|
||||
CoverDevice, SUPPORT_OPEN, SUPPORT_CLOSE, SUPPORT_SET_POSITION,
|
||||
SUPPORT_OPEN_TILT, SUPPORT_CLOSE_TILT, SUPPORT_SET_TILT_POSITION,
|
||||
ATTR_POSITION, ATTR_TILT_POSITION)
|
||||
from homeassistant.const import (
|
||||
STATE_CLOSED, STATE_CLOSING, STATE_OPEN, STATE_OPENING)
|
||||
|
||||
STATE_STOPPED = 'stopped'
|
||||
|
||||
DEPENDENCIES = ['homekit_controller']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CURRENT_GARAGE_STATE_MAP = {
|
||||
0: STATE_OPEN,
|
||||
1: STATE_CLOSED,
|
||||
2: STATE_OPENING,
|
||||
3: STATE_CLOSING,
|
||||
4: STATE_STOPPED
|
||||
}
|
||||
|
||||
TARGET_GARAGE_STATE_MAP = {
|
||||
STATE_OPEN: 0,
|
||||
STATE_CLOSED: 1,
|
||||
STATE_STOPPED: 2
|
||||
}
|
||||
|
||||
CURRENT_WINDOW_STATE_MAP = {
|
||||
0: STATE_OPENING,
|
||||
1: STATE_CLOSING,
|
||||
2: STATE_STOPPED
|
||||
}
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up HomeKit Cover support."""
|
||||
if discovery_info is None:
|
||||
return
|
||||
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
|
||||
|
||||
if discovery_info['device-type'] == 'garage-door-opener':
|
||||
add_entities([HomeKitGarageDoorCover(accessory, discovery_info)],
|
||||
True)
|
||||
else:
|
||||
add_entities([HomeKitWindowCover(accessory, discovery_info)],
|
||||
True)
|
||||
|
||||
|
||||
class HomeKitGarageDoorCover(HomeKitEntity, CoverDevice):
|
||||
"""Representation of a HomeKit Garage Door."""
|
||||
|
||||
def __init__(self, accessory, discovery_info):
|
||||
"""Initialise the Cover."""
|
||||
super().__init__(accessory, discovery_info)
|
||||
self._name = None
|
||||
self._state = None
|
||||
self._obstruction_detected = None
|
||||
self.lock_state = None
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Define this cover as a garage door."""
|
||||
return 'garage'
|
||||
|
||||
def update_characteristics(self, characteristics):
|
||||
"""Synchronise the Cover state with Home Assistant."""
|
||||
# pylint: disable=import-error
|
||||
from homekit.model.characteristics import CharacteristicsTypes
|
||||
|
||||
for characteristic in characteristics:
|
||||
ctype = characteristic['type']
|
||||
ctype = CharacteristicsTypes.get_short(ctype)
|
||||
if ctype == "door-state.current":
|
||||
self._chars['door-state.current'] = \
|
||||
characteristic['iid']
|
||||
self._state = CURRENT_GARAGE_STATE_MAP[characteristic['value']]
|
||||
elif ctype == "door-state.target":
|
||||
self._chars['door-state.target'] = \
|
||||
characteristic['iid']
|
||||
elif ctype == "obstruction-detected":
|
||||
self._chars['obstruction-detected'] = characteristic['iid']
|
||||
self._obstruction_detected = characteristic['value']
|
||||
elif ctype == "name":
|
||||
self._chars['name'] = characteristic['iid']
|
||||
self._name = characteristic['value']
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the cover."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return True if entity is available."""
|
||||
return self._state is not None
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
"""Flag supported features."""
|
||||
return SUPPORT_OPEN | SUPPORT_CLOSE
|
||||
|
||||
@property
|
||||
def is_closed(self):
|
||||
"""Return true if cover is closed, else False."""
|
||||
return self._state == STATE_CLOSED
|
||||
|
||||
@property
|
||||
def is_closing(self):
|
||||
"""Return if the cover is closing or not."""
|
||||
return self._state == STATE_CLOSING
|
||||
|
||||
@property
|
||||
def is_opening(self):
|
||||
"""Return if the cover is opening or not."""
|
||||
return self._state == STATE_OPENING
|
||||
|
||||
def open_cover(self, **kwargs):
|
||||
"""Send open command."""
|
||||
self.set_door_state(STATE_OPEN)
|
||||
|
||||
def close_cover(self, **kwargs):
|
||||
"""Send close command."""
|
||||
self.set_door_state(STATE_CLOSED)
|
||||
|
||||
def set_door_state(self, state):
|
||||
"""Send state command."""
|
||||
characteristics = [{'aid': self._aid,
|
||||
'iid': self._chars['door-state.target'],
|
||||
'value': TARGET_GARAGE_STATE_MAP[state]}]
|
||||
self.put_characteristics(characteristics)
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the optional state attributes."""
|
||||
if self._obstruction_detected is None:
|
||||
return None
|
||||
|
||||
return {
|
||||
'obstruction-detected': self._obstruction_detected,
|
||||
}
|
||||
|
||||
|
||||
class HomeKitWindowCover(HomeKitEntity, CoverDevice):
|
||||
"""Representation of a HomeKit Window or Window Covering."""
|
||||
|
||||
def __init__(self, accessory, discovery_info):
|
||||
"""Initialise the Cover."""
|
||||
super().__init__(accessory, discovery_info)
|
||||
self._name = None
|
||||
self._state = None
|
||||
self._position = None
|
||||
self._tilt_position = None
|
||||
self._hold = None
|
||||
self._obstruction_detected = None
|
||||
self.lock_state = None
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return True if entity is available."""
|
||||
return self._state is not None
|
||||
|
||||
def update_characteristics(self, characteristics):
|
||||
"""Synchronise the Cover state with Home Assistant."""
|
||||
# pylint: disable=import-error
|
||||
from homekit.model.characteristics import CharacteristicsTypes
|
||||
|
||||
for characteristic in characteristics:
|
||||
ctype = characteristic['type']
|
||||
ctype = CharacteristicsTypes.get_short(ctype)
|
||||
if ctype == "position.state":
|
||||
self._chars['position.state'] = \
|
||||
characteristic['iid']
|
||||
if 'value' in characteristic:
|
||||
self._state = \
|
||||
CURRENT_WINDOW_STATE_MAP[characteristic['value']]
|
||||
elif ctype == "position.current":
|
||||
self._chars['position.current'] = \
|
||||
characteristic['iid']
|
||||
self._position = characteristic['value']
|
||||
elif ctype == "position.target":
|
||||
self._chars['position.target'] = \
|
||||
characteristic['iid']
|
||||
elif ctype == "position.hold":
|
||||
self._chars['position.hold'] = characteristic['iid']
|
||||
if 'value' in characteristic:
|
||||
self._hold = characteristic['value']
|
||||
elif ctype == "vertical-tilt.current":
|
||||
self._chars['vertical-tilt.current'] = characteristic['iid']
|
||||
if characteristic['value'] is not None:
|
||||
self._tilt_position = characteristic['value']
|
||||
elif ctype == "horizontal-tilt.current":
|
||||
self._chars['horizontal-tilt.current'] = characteristic['iid']
|
||||
if characteristic['value'] is not None:
|
||||
self._tilt_position = characteristic['value']
|
||||
elif ctype == "vertical-tilt.target":
|
||||
self._chars['vertical-tilt.target'] = \
|
||||
characteristic['iid']
|
||||
elif ctype == "horizontal-tilt.target":
|
||||
self._chars['vertical-tilt.target'] = \
|
||||
characteristic['iid']
|
||||
elif ctype == "obstruction-detected":
|
||||
self._chars['obstruction-detected'] = characteristic['iid']
|
||||
self._obstruction_detected = characteristic['value']
|
||||
elif ctype == "name":
|
||||
self._chars['name'] = characteristic['iid']
|
||||
if 'value' in characteristic:
|
||||
self._name = characteristic['value']
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the cover."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
"""Flag supported features."""
|
||||
supported_features = (
|
||||
SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_SET_POSITION)
|
||||
|
||||
if self._tilt_position is not None:
|
||||
supported_features |= (
|
||||
SUPPORT_OPEN_TILT | SUPPORT_CLOSE_TILT |
|
||||
SUPPORT_SET_TILT_POSITION)
|
||||
|
||||
return supported_features
|
||||
|
||||
@property
|
||||
def current_cover_position(self):
|
||||
"""Return the current position of cover."""
|
||||
return self._position
|
||||
|
||||
@property
|
||||
def is_closed(self):
|
||||
"""Return true if cover is closed, else False."""
|
||||
return self._position == 0
|
||||
|
||||
@property
|
||||
def is_closing(self):
|
||||
"""Return if the cover is closing or not."""
|
||||
return self._state == STATE_CLOSING
|
||||
|
||||
@property
|
||||
def is_opening(self):
|
||||
"""Return if the cover is opening or not."""
|
||||
return self._state == STATE_OPENING
|
||||
|
||||
def open_cover(self, **kwargs):
|
||||
"""Send open command."""
|
||||
self.set_cover_position(position=100)
|
||||
|
||||
def close_cover(self, **kwargs):
|
||||
"""Send close command."""
|
||||
self.set_cover_position(position=0)
|
||||
|
||||
def set_cover_position(self, **kwargs):
|
||||
"""Send position command."""
|
||||
position = kwargs[ATTR_POSITION]
|
||||
characteristics = [{'aid': self._aid,
|
||||
'iid': self._chars['position.target'],
|
||||
'value': position}]
|
||||
self.put_characteristics(characteristics)
|
||||
|
||||
@property
|
||||
def current_cover_tilt_position(self):
|
||||
"""Return current position of cover tilt."""
|
||||
return self._tilt_position
|
||||
|
||||
def set_cover_tilt_position(self, **kwargs):
|
||||
"""Move the cover tilt to a specific position."""
|
||||
tilt_position = kwargs[ATTR_TILT_POSITION]
|
||||
if 'vertical-tilt.target' in self._chars:
|
||||
characteristics = [{'aid': self._aid,
|
||||
'iid': self._chars['vertical-tilt.target'],
|
||||
'value': tilt_position}]
|
||||
self.put_characteristics(characteristics)
|
||||
elif 'horizontal-tilt.target' in self._chars:
|
||||
characteristics = [{'aid': self._aid,
|
||||
'iid':
|
||||
self._chars['horizontal-tilt.target'],
|
||||
'value': tilt_position}]
|
||||
self.put_characteristics(characteristics)
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the optional state attributes."""
|
||||
state_attributes = {}
|
||||
if self._obstruction_detected is not None:
|
||||
state_attributes['obstruction-detected'] = \
|
||||
self._obstruction_detected
|
||||
|
||||
if self._hold is not None:
|
||||
state_attributes['hold-position'] = \
|
||||
self._hold
|
||||
|
||||
return state_attributes
|
||||
@@ -0,0 +1,131 @@
|
||||
"""
|
||||
Support for Homekit lights.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/light.homekit_controller/
|
||||
"""
|
||||
import logging
|
||||
|
||||
from homeassistant.components.homekit_controller import (
|
||||
HomeKitEntity, KNOWN_ACCESSORIES)
|
||||
from homeassistant.components.light import (
|
||||
ATTR_BRIGHTNESS, ATTR_HS_COLOR, ATTR_COLOR_TEMP, SUPPORT_BRIGHTNESS,
|
||||
SUPPORT_COLOR, SUPPORT_COLOR_TEMP, Light)
|
||||
|
||||
DEPENDENCIES = ['homekit_controller']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up Homekit lighting."""
|
||||
if discovery_info is not None:
|
||||
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
|
||||
add_entities([HomeKitLight(accessory, discovery_info)], True)
|
||||
|
||||
|
||||
class HomeKitLight(HomeKitEntity, Light):
|
||||
"""Representation of a Homekit light."""
|
||||
|
||||
def __init__(self, *args):
|
||||
"""Initialise the light."""
|
||||
super().__init__(*args)
|
||||
self._on = None
|
||||
self._brightness = None
|
||||
self._color_temperature = None
|
||||
self._hue = None
|
||||
self._saturation = None
|
||||
|
||||
def update_characteristics(self, characteristics):
|
||||
"""Synchronise light state with Home Assistant."""
|
||||
# pylint: disable=import-error
|
||||
from homekit.model.characteristics import CharacteristicsTypes
|
||||
|
||||
for characteristic in characteristics:
|
||||
ctype = characteristic['type']
|
||||
ctype = CharacteristicsTypes.get_short(ctype)
|
||||
if ctype == "on":
|
||||
self._chars['on'] = characteristic['iid']
|
||||
self._on = characteristic['value']
|
||||
elif ctype == 'brightness':
|
||||
self._chars['brightness'] = characteristic['iid']
|
||||
self._features |= SUPPORT_BRIGHTNESS
|
||||
self._brightness = characteristic['value']
|
||||
elif ctype == 'color-temperature':
|
||||
self._chars['color_temperature'] = characteristic['iid']
|
||||
self._features |= SUPPORT_COLOR_TEMP
|
||||
self._color_temperature = characteristic['value']
|
||||
elif ctype == "hue":
|
||||
self._chars['hue'] = characteristic['iid']
|
||||
self._features |= SUPPORT_COLOR
|
||||
self._hue = characteristic['value']
|
||||
elif ctype == "saturation":
|
||||
self._chars['saturation'] = characteristic['iid']
|
||||
self._features |= SUPPORT_COLOR
|
||||
self._saturation = characteristic['value']
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if device is on."""
|
||||
return self._on
|
||||
|
||||
@property
|
||||
def brightness(self):
|
||||
"""Return the brightness of this light between 0..255."""
|
||||
if self._features & SUPPORT_BRIGHTNESS:
|
||||
return self._brightness * 255 / 100
|
||||
return None
|
||||
|
||||
@property
|
||||
def hs_color(self):
|
||||
"""Return the color property."""
|
||||
if self._features & SUPPORT_COLOR:
|
||||
return (self._hue, self._saturation)
|
||||
return None
|
||||
|
||||
@property
|
||||
def color_temp(self):
|
||||
"""Return the color temperature."""
|
||||
if self._features & SUPPORT_COLOR_TEMP:
|
||||
return self._color_temperature
|
||||
return None
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
"""Flag supported features."""
|
||||
return self._features
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the specified light on."""
|
||||
hs_color = kwargs.get(ATTR_HS_COLOR)
|
||||
temperature = kwargs.get(ATTR_COLOR_TEMP)
|
||||
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
||||
|
||||
characteristics = []
|
||||
if hs_color is not None:
|
||||
characteristics.append({'aid': self._aid,
|
||||
'iid': self._chars['hue'],
|
||||
'value': hs_color[0]})
|
||||
characteristics.append({'aid': self._aid,
|
||||
'iid': self._chars['saturation'],
|
||||
'value': hs_color[1]})
|
||||
if brightness is not None:
|
||||
characteristics.append({'aid': self._aid,
|
||||
'iid': self._chars['brightness'],
|
||||
'value': int(brightness * 100 / 255)})
|
||||
|
||||
if temperature is not None:
|
||||
characteristics.append({'aid': self._aid,
|
||||
'iid': self._chars['color-temperature'],
|
||||
'value': int(temperature)})
|
||||
characteristics.append({'aid': self._aid,
|
||||
'iid': self._chars['on'],
|
||||
'value': True})
|
||||
self.put_characteristics(characteristics)
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the specified light off."""
|
||||
characteristics = [{'aid': self._aid,
|
||||
'iid': self._chars['on'],
|
||||
'value': False}]
|
||||
self.put_characteristics(characteristics)
|
||||
@@ -0,0 +1,111 @@
|
||||
"""
|
||||
Support for HomeKit Controller locks.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/lock.homekit_controller/
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from homeassistant.components.homekit_controller import (HomeKitEntity,
|
||||
KNOWN_ACCESSORIES)
|
||||
from homeassistant.components.lock import LockDevice
|
||||
from homeassistant.const import (STATE_LOCKED, STATE_UNLOCKED,
|
||||
ATTR_BATTERY_LEVEL)
|
||||
|
||||
DEPENDENCIES = ['homekit_controller']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
STATE_JAMMED = 'jammed'
|
||||
|
||||
CURRENT_STATE_MAP = {
|
||||
0: STATE_UNLOCKED,
|
||||
1: STATE_LOCKED,
|
||||
2: STATE_JAMMED,
|
||||
3: None,
|
||||
}
|
||||
|
||||
TARGET_STATE_MAP = {
|
||||
STATE_UNLOCKED: 0,
|
||||
STATE_LOCKED: 1
|
||||
}
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up Homekit Lock support."""
|
||||
if discovery_info is None:
|
||||
return
|
||||
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
|
||||
add_entities([HomeKitLock(accessory, discovery_info)],
|
||||
True)
|
||||
|
||||
|
||||
class HomeKitLock(HomeKitEntity, LockDevice):
|
||||
"""Representation of a HomeKit Controller Lock."""
|
||||
|
||||
def __init__(self, accessory, discovery_info):
|
||||
"""Initialise the Lock."""
|
||||
super().__init__(accessory, discovery_info)
|
||||
self._state = None
|
||||
self._name = discovery_info['model']
|
||||
self._battery_level = None
|
||||
|
||||
def update_characteristics(self, characteristics):
|
||||
"""Synchronise the Lock state with Home Assistant."""
|
||||
# pylint: disable=import-error
|
||||
from homekit.model.characteristics import CharacteristicsTypes
|
||||
|
||||
for characteristic in characteristics:
|
||||
ctype = characteristic['type']
|
||||
ctype = CharacteristicsTypes.get_short(ctype)
|
||||
if ctype == "lock-mechanism.current-state":
|
||||
self._chars['lock-mechanism.current-state'] = \
|
||||
characteristic['iid']
|
||||
self._state = CURRENT_STATE_MAP[characteristic['value']]
|
||||
elif ctype == "lock-mechanism.target-state":
|
||||
self._chars['lock-mechanism.target-state'] = \
|
||||
characteristic['iid']
|
||||
elif ctype == "battery-level":
|
||||
self._chars['battery-level'] = characteristic['iid']
|
||||
self._battery_level = characteristic['value']
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of this device."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def is_locked(self):
|
||||
"""Return true if device is locked."""
|
||||
return self._state == STATE_LOCKED
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return True if entity is available."""
|
||||
return self._state is not None
|
||||
|
||||
def lock(self, **kwargs):
|
||||
"""Lock the device."""
|
||||
self._set_lock_state(STATE_LOCKED)
|
||||
|
||||
def unlock(self, **kwargs):
|
||||
"""Unlock the device."""
|
||||
self._set_lock_state(STATE_UNLOCKED)
|
||||
|
||||
def _set_lock_state(self, state):
|
||||
"""Send state command."""
|
||||
characteristics = [{'aid': self._aid,
|
||||
'iid': self._chars['lock-mechanism.target-state'],
|
||||
'value': TARGET_STATE_MAP[state]}]
|
||||
self.put_characteristics(characteristics)
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the optional state attributes."""
|
||||
if self._battery_level is None:
|
||||
return None
|
||||
|
||||
return {
|
||||
ATTR_BATTERY_LEVEL: self._battery_level,
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
"""
|
||||
Support for Homekit switches.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/switch.homekit_controller/
|
||||
"""
|
||||
import logging
|
||||
|
||||
from homeassistant.components.homekit_controller import (HomeKitEntity,
|
||||
KNOWN_ACCESSORIES)
|
||||
from homeassistant.components.switch import SwitchDevice
|
||||
|
||||
DEPENDENCIES = ['homekit_controller']
|
||||
|
||||
OUTLET_IN_USE = "outlet_in_use"
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up Homekit switch support."""
|
||||
if discovery_info is not None:
|
||||
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
|
||||
add_entities([HomeKitSwitch(accessory, discovery_info)], True)
|
||||
|
||||
|
||||
class HomeKitSwitch(HomeKitEntity, SwitchDevice):
|
||||
"""Representation of a Homekit switch."""
|
||||
|
||||
def __init__(self, *args):
|
||||
"""Initialise the switch."""
|
||||
super().__init__(*args)
|
||||
self._on = None
|
||||
self._outlet_in_use = None
|
||||
|
||||
def update_characteristics(self, characteristics):
|
||||
"""Synchronise the switch state with Home Assistant."""
|
||||
# pylint: disable=import-error
|
||||
from homekit.model.characteristics import CharacteristicsTypes
|
||||
|
||||
for characteristic in characteristics:
|
||||
ctype = characteristic['type']
|
||||
ctype = CharacteristicsTypes.get_short(ctype)
|
||||
if ctype == "on":
|
||||
self._chars['on'] = characteristic['iid']
|
||||
self._on = characteristic['value']
|
||||
elif ctype == "outlet-in-use":
|
||||
self._chars['outlet-in-use'] = characteristic['iid']
|
||||
self._outlet_in_use = characteristic['value']
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if device is on."""
|
||||
return self._on
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the specified switch on."""
|
||||
self._on = True
|
||||
characteristics = [{'aid': self._aid,
|
||||
'iid': self._chars['on'],
|
||||
'value': True}]
|
||||
self.put_characteristics(characteristics)
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the specified switch off."""
|
||||
characteristics = [{'aid': self._aid,
|
||||
'iid': self._chars['on'],
|
||||
'value': False}]
|
||||
self.put_characteristics(characteristics)
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the optional state attributes."""
|
||||
if self._outlet_in_use is not None:
|
||||
return {
|
||||
OUTLET_IN_USE: self._outlet_in_use,
|
||||
}
|
||||
Reference in new issue
Block a user