2019-03-22 18:59:10 +00:00
|
|
|
"""Entity to track connections to websocket API."""
|
2021-05-21 16:39:18 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-03-22 18:50:29 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2021-05-21 16:39:18 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-03-24 12:19:11 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-05-21 16:39:18 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-01-24 17:51:06 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2019-03-22 18:59:10 +00:00
|
|
|
|
2019-04-13 17:48:40 +00:00
|
|
|
from .const import (
|
2019-12-08 11:19:15 +00:00
|
|
|
DATA_CONNECTIONS,
|
2019-07-31 19:25:30 +00:00
|
|
|
SIGNAL_WEBSOCKET_CONNECTED,
|
|
|
|
SIGNAL_WEBSOCKET_DISCONNECTED,
|
|
|
|
)
|
2019-03-22 18:59:10 +00:00
|
|
|
|
2019-09-29 17:07:49 +00:00
|
|
|
|
2021-05-21 16:39:18 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2022-01-24 17:51:06 +00:00
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
2021-05-21 16:39:18 +00:00
|
|
|
) -> None:
|
2019-03-22 18:59:10 +00:00
|
|
|
"""Set up the API streams platform."""
|
|
|
|
entity = APICount()
|
|
|
|
|
|
|
|
async_add_entities([entity])
|
|
|
|
|
|
|
|
|
2021-03-22 18:50:29 +00:00
|
|
|
class APICount(SensorEntity):
|
2019-03-22 18:59:10 +00:00
|
|
|
"""Entity to represent how many people are connected to the stream API."""
|
|
|
|
|
2021-05-21 16:39:18 +00:00
|
|
|
def __init__(self) -> None:
|
2019-03-22 18:59:10 +00:00
|
|
|
"""Initialize the API count."""
|
2020-06-01 05:18:30 +00:00
|
|
|
self.count = 0
|
2019-04-13 17:48:40 +00:00
|
|
|
|
2021-05-21 16:39:18 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2019-04-13 17:48:40 +00:00
|
|
|
"""Added to hass."""
|
2020-04-02 16:25:33 +00:00
|
|
|
self.async_on_remove(
|
2022-03-24 12:19:11 +00:00
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_WEBSOCKET_CONNECTED, self._update_count
|
2020-04-02 16:25:33 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-04-02 16:25:33 +00:00
|
|
|
self.async_on_remove(
|
2022-03-24 12:19:11 +00:00
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_WEBSOCKET_DISCONNECTED, self._update_count
|
2020-04-02 16:25:33 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-03-22 18:59:10 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-21 16:39:18 +00:00
|
|
|
def name(self) -> str:
|
2019-03-22 18:59:10 +00:00
|
|
|
"""Return name of entity."""
|
|
|
|
return "Connected clients"
|
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:16 +00:00
|
|
|
def native_value(self) -> int:
|
2019-03-22 18:59:10 +00:00
|
|
|
"""Return current API count."""
|
|
|
|
return self.count
|
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:16 +00:00
|
|
|
def native_unit_of_measurement(self) -> str:
|
2019-03-22 18:59:10 +00:00
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return "clients"
|
|
|
|
|
|
|
|
@callback
|
2021-05-21 16:39:18 +00:00
|
|
|
def _update_count(self) -> None:
|
2019-04-13 17:48:40 +00:00
|
|
|
self.count = self.hass.data.get(DATA_CONNECTIONS, 0)
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|