Fix websocket connection sensor (#22923)
* Fix for #22890 * Singleton count
This commit is contained in:
parent
479511ee42
commit
2527731865
3 changed files with 22 additions and 16 deletions
|
@ -24,3 +24,6 @@ CANCELLATION_ERRORS = (asyncio.CancelledError, futures.CancelledError)
|
||||||
# Event types
|
# Event types
|
||||||
SIGNAL_WEBSOCKET_CONNECTED = 'websocket_connected'
|
SIGNAL_WEBSOCKET_CONNECTED = 'websocket_connected'
|
||||||
SIGNAL_WEBSOCKET_DISCONNECTED = 'websocket_disconnected'
|
SIGNAL_WEBSOCKET_DISCONNECTED = 'websocket_disconnected'
|
||||||
|
|
||||||
|
# Data used to store the current connection list
|
||||||
|
DATA_CONNECTIONS = DOMAIN + '.connections'
|
||||||
|
|
|
@ -15,7 +15,8 @@ from homeassistant.helpers.json import JSONEncoder
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
MAX_PENDING_MSG, CANCELLATION_ERRORS, URL, ERR_UNKNOWN_ERROR,
|
MAX_PENDING_MSG, CANCELLATION_ERRORS, URL, ERR_UNKNOWN_ERROR,
|
||||||
SIGNAL_WEBSOCKET_CONNECTED, SIGNAL_WEBSOCKET_DISCONNECTED)
|
SIGNAL_WEBSOCKET_CONNECTED, SIGNAL_WEBSOCKET_DISCONNECTED,
|
||||||
|
DATA_CONNECTIONS)
|
||||||
from .auth import AuthPhase, auth_required_message
|
from .auth import AuthPhase, auth_required_message
|
||||||
from .error import Disconnect
|
from .error import Disconnect
|
||||||
from .messages import error_message
|
from .messages import error_message
|
||||||
|
@ -145,6 +146,8 @@ class WebSocketHandler:
|
||||||
|
|
||||||
self._logger.debug("Received %s", msg)
|
self._logger.debug("Received %s", msg)
|
||||||
connection = await auth.async_handle(msg)
|
connection = await auth.async_handle(msg)
|
||||||
|
self.hass.data[DATA_CONNECTIONS] = \
|
||||||
|
self.hass.data.get(DATA_CONNECTIONS, 0) + 1
|
||||||
self.hass.helpers.dispatcher.async_dispatcher_send(
|
self.hass.helpers.dispatcher.async_dispatcher_send(
|
||||||
SIGNAL_WEBSOCKET_CONNECTED)
|
SIGNAL_WEBSOCKET_CONNECTED)
|
||||||
|
|
||||||
|
@ -197,6 +200,7 @@ class WebSocketHandler:
|
||||||
else:
|
else:
|
||||||
self._logger.warning("Disconnected: %s", disconnect_warn)
|
self._logger.warning("Disconnected: %s", disconnect_warn)
|
||||||
|
|
||||||
|
self.hass.data[DATA_CONNECTIONS] -= 1
|
||||||
self.hass.helpers.dispatcher.async_dispatcher_send(
|
self.hass.helpers.dispatcher.async_dispatcher_send(
|
||||||
SIGNAL_WEBSOCKET_DISCONNECTED)
|
SIGNAL_WEBSOCKET_DISCONNECTED)
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,9 @@
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
from .const import SIGNAL_WEBSOCKET_CONNECTED, SIGNAL_WEBSOCKET_DISCONNECTED
|
from .const import (
|
||||||
|
SIGNAL_WEBSOCKET_CONNECTED, SIGNAL_WEBSOCKET_DISCONNECTED,
|
||||||
|
DATA_CONNECTIONS)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(
|
async def async_setup_platform(
|
||||||
|
@ -11,12 +13,6 @@ async def async_setup_platform(
|
||||||
"""Set up the API streams platform."""
|
"""Set up the API streams platform."""
|
||||||
entity = APICount()
|
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])
|
async_add_entities([entity])
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,7 +21,15 @@ class APICount(Entity):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""Initialize the API count."""
|
"""Initialize the API count."""
|
||||||
self.count = 0
|
self.count = None
|
||||||
|
|
||||||
|
async def async_added_to_hass(self):
|
||||||
|
"""Added to hass."""
|
||||||
|
self.hass.helpers.dispatcher.async_dispatcher_connect(
|
||||||
|
SIGNAL_WEBSOCKET_CONNECTED, self._update_count)
|
||||||
|
self.hass.helpers.dispatcher.async_dispatcher_connect(
|
||||||
|
SIGNAL_WEBSOCKET_DISCONNECTED, self._update_count)
|
||||||
|
self._update_count()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
@ -43,11 +47,6 @@ class APICount(Entity):
|
||||||
return "clients"
|
return "clients"
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _increment(self):
|
def _update_count(self):
|
||||||
self.count += 1
|
self.count = self.hass.data.get(DATA_CONNECTIONS, 0)
|
||||||
self.async_schedule_update_ha_state()
|
|
||||||
|
|
||||||
@callback
|
|
||||||
def _decrement(self):
|
|
||||||
self.count -= 1
|
|
||||||
self.async_schedule_update_ha_state()
|
self.async_schedule_update_ha_state()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue