Fixing the api_streams sensor (#22200)
* Fire events with websocket messages. * Added tests to validate * Fixed api_streams sensor to use new sensor * Delete from coverageac as now works. * Removed websocket request event. * Use dispatcher instead of events. * Moved sensor to under websocket_api * Changes as per code review * Fixed tests. * Modified test * Patch
This commit is contained in:
1 parent
2b6e197deb
commit
1ddc65a0ce
8 files changed
+142
-168
No files matched your search
@@ -20,3 +20,7 @@ TYPE_RESULT = 'result'
|
||||
# Originally, this was just asyncio.CancelledError, but issue #9546 showed
|
||||
# that futures.CancelledErrors can also occur in some situations.
|
||||
CANCELLATION_ERRORS = (asyncio.CancelledError, futures.CancelledError)
|
||||
|
||||
# Event types
|
||||
SIGNAL_WEBSOCKET_CONNECTED = 'websocket_connected'
|
||||
SIGNAL_WEBSOCKET_DISCONNECTED = 'websocket_disconnected'
|
||||
@@ -13,7 +13,9 @@ from homeassistant.core import callback
|
||||
from homeassistant.components.http import HomeAssistantView
|
||||
from homeassistant.helpers.json import JSONEncoder
|
||||
|
||||
from .const import MAX_PENDING_MSG, CANCELLATION_ERRORS, URL, ERR_UNKNOWN_ERROR
|
||||
from .const import (
|
||||
MAX_PENDING_MSG, CANCELLATION_ERRORS, URL, ERR_UNKNOWN_ERROR,
|
||||
SIGNAL_WEBSOCKET_CONNECTED, SIGNAL_WEBSOCKET_DISCONNECTED)
|
||||
from .auth import AuthPhase, auth_required_message
|
||||
from .error import Disconnect
|
||||
from .messages import error_message
|
||||
@@ -142,6 +144,8 @@ class WebSocketHandler:
|
||||
|
||||
self._logger.debug("Received %s", msg)
|
||||
connection = await auth.async_handle(msg)
|
||||
self.hass.helpers.dispatcher.async_dispatcher_send(
|
||||
SIGNAL_WEBSOCKET_CONNECTED)
|
||||
|
||||
# Command phase
|
||||
while not wsock.closed:
|
||||
@@ -192,4 +196,7 @@ class WebSocketHandler:
|
||||
else:
|
||||
self._logger.warning("Disconnected: %s", disconnect_warn)
|
||||
|
||||
self.hass.helpers.dispatcher.async_dispatcher_send(
|
||||
SIGNAL_WEBSOCKET_DISCONNECTED)
|
||||
|
||||
return wsock
|
||||
@@ -0,0 +1,53 @@
|
||||
"""Entity to track connections to websocket API."""
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
from .const import SIGNAL_WEBSOCKET_CONNECTED, SIGNAL_WEBSOCKET_DISCONNECTED
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up the API streams platform."""
|
||||
entity = APICount()
|
||||
|
||||
# pylint: disable=protected-access
|
||||
hass.helpers.dispatcher.async_dispatcher_connect(
|
||||
SIGNAL_WEBSOCKET_CONNECTED, entity._increment)
|
||||
hass.helpers.dispatcher.async_dispatcher_connect(
|
||||
SIGNAL_WEBSOCKET_DISCONNECTED, entity._decrement)
|
||||
|
||||
async_add_entities([entity])
|
||||
|
||||
|
||||
class APICount(Entity):
|
||||
"""Entity to represent how many people are connected to the stream API."""
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the API count."""
|
||||
self.count = 0
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return name of entity."""
|
||||
return "Connected clients"
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return current API count."""
|
||||
return self.count
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit of measurement."""
|
||||
return "clients"
|
||||
|
||||
@callback
|
||||
def _increment(self):
|
||||
self.count += 1
|
||||
self.async_schedule_update_ha_state()
|
||||
|
||||
@callback
|
||||
def _decrement(self):
|
||||
self.count -= 1
|
||||
self.async_schedule_update_ha_state()
|
||||
Reference in new issue
Block a user