mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Sense update (#24220)
* Changed updates to be done by component and updates dispatched * syntax updates * Added code owner * Removed whitespace * Updated CODEOWNERS * Added subscription undoer
This commit is contained in:
parent
ef820c3126
commit
5a81ddd4e7
@ -202,6 +202,7 @@ homeassistant/components/ruter/* @ludeeus
|
||||
homeassistant/components/scene/* @home-assistant/core
|
||||
homeassistant/components/scrape/* @fabaff
|
||||
homeassistant/components/script/* @home-assistant/core
|
||||
homeassistant/components/sense/* @kbickar
|
||||
homeassistant/components/sensibo/* @andrey-git
|
||||
homeassistant/components/serial/* @fabaff
|
||||
homeassistant/components/seventeentrack/* @bachya
|
||||
|
@ -1,11 +1,14 @@
|
||||
"""Support for monitoring a Sense energy sensor."""
|
||||
import logging
|
||||
from datetime import timedelta
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_TIMEOUT
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.discovery import async_load_platform
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.event import async_track_time_interval
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -15,6 +18,7 @@ DEFAULT_TIMEOUT = 5
|
||||
DOMAIN = 'sense'
|
||||
|
||||
SENSE_DATA = 'sense_data'
|
||||
SENSE_DEVICE_UPDATE = 'sense_devices_update'
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({
|
||||
@ -27,7 +31,9 @@ CONFIG_SCHEMA = vol.Schema({
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Set up the Sense sensor."""
|
||||
from sense_energy import ASyncSenseable, SenseAuthenticationException
|
||||
from sense_energy import (
|
||||
ASyncSenseable, SenseAuthenticationException,
|
||||
SenseAPITimeoutException)
|
||||
|
||||
username = config[DOMAIN][CONF_EMAIL]
|
||||
password = config[DOMAIN][CONF_PASSWORD]
|
||||
@ -45,4 +51,15 @@ async def async_setup(hass, config):
|
||||
async_load_platform(hass, 'sensor', DOMAIN, {}, config))
|
||||
hass.async_create_task(
|
||||
async_load_platform(hass, 'binary_sensor', DOMAIN, {}, config))
|
||||
|
||||
async def async_sense_update(now):
|
||||
"""Retrieve latest state."""
|
||||
try:
|
||||
await hass.data[SENSE_DATA].update_realtime()
|
||||
async_dispatcher_send(hass, SENSE_DEVICE_UPDATE)
|
||||
except SenseAPITimeoutException:
|
||||
_LOGGER.error("Timeout retrieving data")
|
||||
|
||||
async_track_time_interval(hass, async_sense_update,
|
||||
timedelta(seconds=ACTIVE_UPDATE_RATE))
|
||||
return True
|
||||
|
@ -2,8 +2,10 @@
|
||||
import logging
|
||||
|
||||
from homeassistant.components.binary_sensor import BinarySensorDevice
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.core import callback
|
||||
|
||||
from . import SENSE_DATA
|
||||
from . import SENSE_DATA, SENSE_DEVICE_UPDATE
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -75,12 +77,12 @@ class SenseDevice(BinarySensorDevice):
|
||||
self._id = device['id']
|
||||
self._icon = sense_to_mdi(device['icon'])
|
||||
self._data = data
|
||||
self._state = False
|
||||
self._undo_dispatch_subscription = None
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if the binary sensor is on."""
|
||||
return self._state
|
||||
return self._name in self._data.active_devices
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
@ -102,12 +104,22 @@ class SenseDevice(BinarySensorDevice):
|
||||
"""Return the device class of the binary sensor."""
|
||||
return BIN_SENSOR_CLASS
|
||||
|
||||
async def async_update(self):
|
||||
"""Retrieve latest state."""
|
||||
from sense_energy.sense_api import SenseAPITimeoutException
|
||||
try:
|
||||
await self._data.update_realtime()
|
||||
except SenseAPITimeoutException:
|
||||
_LOGGER.error("Timeout retrieving data")
|
||||
return
|
||||
self._state = self._name in self._data.active_devices
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Return the deviceshould not poll for updates."""
|
||||
return False
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Register callbacks."""
|
||||
@callback
|
||||
def update():
|
||||
"""Update the state."""
|
||||
self.async_schedule_update_ha_state(True)
|
||||
|
||||
self._undo_dispatch_subscription = async_dispatcher_connect(
|
||||
self.hass, SENSE_DEVICE_UPDATE, update)
|
||||
|
||||
async def async_will_remove_from_hass(self):
|
||||
"""Undo subscription."""
|
||||
if self._undo_dispatch_subscription:
|
||||
self._undo_dispatch_subscription()
|
||||
|
@ -6,5 +6,5 @@
|
||||
"sense_energy==0.7.0"
|
||||
],
|
||||
"dependencies": [],
|
||||
"codeowners": []
|
||||
"codeowners": ["@kbickar"]
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user