mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +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/scene/* @home-assistant/core
|
||||||
homeassistant/components/scrape/* @fabaff
|
homeassistant/components/scrape/* @fabaff
|
||||||
homeassistant/components/script/* @home-assistant/core
|
homeassistant/components/script/* @home-assistant/core
|
||||||
|
homeassistant/components/sense/* @kbickar
|
||||||
homeassistant/components/sensibo/* @andrey-git
|
homeassistant/components/sensibo/* @andrey-git
|
||||||
homeassistant/components/serial/* @fabaff
|
homeassistant/components/serial/* @fabaff
|
||||||
homeassistant/components/seventeentrack/* @bachya
|
homeassistant/components/seventeentrack/* @bachya
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
"""Support for monitoring a Sense energy sensor."""
|
"""Support for monitoring a Sense energy sensor."""
|
||||||
import logging
|
import logging
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_TIMEOUT
|
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_TIMEOUT
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.discovery import async_load_platform
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -15,6 +18,7 @@ DEFAULT_TIMEOUT = 5
|
|||||||
DOMAIN = 'sense'
|
DOMAIN = 'sense'
|
||||||
|
|
||||||
SENSE_DATA = 'sense_data'
|
SENSE_DATA = 'sense_data'
|
||||||
|
SENSE_DEVICE_UPDATE = 'sense_devices_update'
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema({
|
CONFIG_SCHEMA = vol.Schema({
|
||||||
DOMAIN: vol.Schema({
|
DOMAIN: vol.Schema({
|
||||||
@ -27,7 +31,9 @@ CONFIG_SCHEMA = vol.Schema({
|
|||||||
|
|
||||||
async def async_setup(hass, config):
|
async def async_setup(hass, config):
|
||||||
"""Set up the Sense sensor."""
|
"""Set up the Sense sensor."""
|
||||||
from sense_energy import ASyncSenseable, SenseAuthenticationException
|
from sense_energy import (
|
||||||
|
ASyncSenseable, SenseAuthenticationException,
|
||||||
|
SenseAPITimeoutException)
|
||||||
|
|
||||||
username = config[DOMAIN][CONF_EMAIL]
|
username = config[DOMAIN][CONF_EMAIL]
|
||||||
password = config[DOMAIN][CONF_PASSWORD]
|
password = config[DOMAIN][CONF_PASSWORD]
|
||||||
@ -45,4 +51,15 @@ async def async_setup(hass, config):
|
|||||||
async_load_platform(hass, 'sensor', DOMAIN, {}, config))
|
async_load_platform(hass, 'sensor', DOMAIN, {}, config))
|
||||||
hass.async_create_task(
|
hass.async_create_task(
|
||||||
async_load_platform(hass, 'binary_sensor', DOMAIN, {}, config))
|
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
|
return True
|
||||||
|
@ -2,8 +2,10 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import BinarySensorDevice
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -75,12 +77,12 @@ class SenseDevice(BinarySensorDevice):
|
|||||||
self._id = device['id']
|
self._id = device['id']
|
||||||
self._icon = sense_to_mdi(device['icon'])
|
self._icon = sense_to_mdi(device['icon'])
|
||||||
self._data = data
|
self._data = data
|
||||||
self._state = False
|
self._undo_dispatch_subscription = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return true if the binary sensor is on."""
|
"""Return true if the binary sensor is on."""
|
||||||
return self._state
|
return self._name in self._data.active_devices
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -102,12 +104,22 @@ class SenseDevice(BinarySensorDevice):
|
|||||||
"""Return the device class of the binary sensor."""
|
"""Return the device class of the binary sensor."""
|
||||||
return BIN_SENSOR_CLASS
|
return BIN_SENSOR_CLASS
|
||||||
|
|
||||||
async def async_update(self):
|
@property
|
||||||
"""Retrieve latest state."""
|
def should_poll(self):
|
||||||
from sense_energy.sense_api import SenseAPITimeoutException
|
"""Return the deviceshould not poll for updates."""
|
||||||
try:
|
return False
|
||||||
await self._data.update_realtime()
|
|
||||||
except SenseAPITimeoutException:
|
async def async_added_to_hass(self):
|
||||||
_LOGGER.error("Timeout retrieving data")
|
"""Register callbacks."""
|
||||||
return
|
@callback
|
||||||
self._state = self._name in self._data.active_devices
|
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"
|
"sense_energy==0.7.0"
|
||||||
],
|
],
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"codeowners": []
|
"codeowners": ["@kbickar"]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user