1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Add linked battery sensor to HomeKit (#22788)

This commit is contained in:
Austin Drummond
2019-04-09 17:13:48 -04:00
committed by cdce8p
parent 6244a397b1
commit c4e31bc4df
5 changed files with 103 additions and 16 deletions

View File

@@ -19,8 +19,8 @@ from homeassistant.util import dt as dt_util
from .const import (
ATTR_DISPLAY_NAME, ATTR_VALUE, BRIDGE_MODEL, BRIDGE_SERIAL_NUMBER,
CHAR_BATTERY_LEVEL, CHAR_CHARGING_STATE, CHAR_STATUS_LOW_BATTERY,
DEBOUNCE_TIMEOUT, EVENT_HOMEKIT_CHANGED, MANUFACTURER,
SERV_BATTERY_SERVICE)
CONF_LINKED_BATTERY_SENSOR, DEBOUNCE_TIMEOUT, EVENT_HOMEKIT_CHANGED,
MANUFACTURER, SERV_BATTERY_SERVICE)
from .util import convert_to_float, dismiss_setup_message, show_setup_message
_LOGGER = logging.getLogger(__name__)
@@ -65,19 +65,25 @@ class HomeAccessory(Accessory):
firmware_revision=__version__, manufacturer=MANUFACTURER,
model=model, serial_number=entity_id)
self.category = category
self.config = config
self.config = config or {}
self.entity_id = entity_id
self.hass = hass
self.debounce = {}
self._support_battery_level = False
self._support_battery_charging = True
self.linked_battery_sensor = \
self.config.get(CONF_LINKED_BATTERY_SENSOR)
"""Add battery service if available"""
battery_level = self.hass.states.get(self.entity_id).attributes \
battery_found = self.hass.states.get(self.entity_id).attributes \
.get(ATTR_BATTERY_LEVEL)
if battery_level is None:
if self.linked_battery_sensor:
battery_found = self.hass.states.get(
self.linked_battery_sensor).state
if battery_found is None:
return
_LOGGER.debug('%s: Found battery level attribute', self.entity_id)
_LOGGER.debug('%s: Found battery level', self.entity_id)
self._support_battery_level = True
serv_battery = self.add_preload_service(SERV_BATTERY_SERVICE)
self._char_battery = serv_battery.configure_char(
@@ -104,6 +110,14 @@ class HomeAccessory(Accessory):
async_track_state_change(
self.hass, self.entity_id, self.update_state_callback)
if self.linked_battery_sensor:
battery_state = self.hass.states.get(self.linked_battery_sensor)
self.hass.async_add_job(self.update_linked_battery, None, None,
battery_state)
async_track_state_change(
self.hass, self.linked_battery_sensor,
self.update_linked_battery)
@ha_callback
def update_state_callback(self, entity_id=None, old_state=None,
new_state=None):
@@ -111,10 +125,16 @@ class HomeAccessory(Accessory):
_LOGGER.debug('New_state: %s', new_state)
if new_state is None:
return
if self._support_battery_level:
if self._support_battery_level and not self.linked_battery_sensor:
self.hass.async_add_executor_job(self.update_battery, new_state)
self.hass.async_add_executor_job(self.update_state, new_state)
@ha_callback
def update_linked_battery(self, entity_id=None, old_state=None,
new_state=None):
"""Handle linked battery sensor state change listener callback."""
self.hass.async_add_executor_job(self.update_battery, new_state)
def update_battery(self, new_state):
"""Update battery service if available.
@@ -122,6 +142,8 @@ class HomeAccessory(Accessory):
"""
battery_level = convert_to_float(
new_state.attributes.get(ATTR_BATTERY_LEVEL))
if self.linked_battery_sensor:
battery_level = convert_to_float(new_state.state)
if battery_level is None:
return
self._char_battery.set_value(battery_level)