Files
homeassistant-core/homeassistant/components/websocket_api/sensor.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

57 lines
1.7 KiB
Python
Raw Normal View History

2019-03-23 02:59:10 +08:00
"""Entity to track connections to websocket API."""
from __future__ import annotations
from homeassistant.components.sensor import SensorEntity
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
2019-03-23 02:59:10 +08:00
2019-04-14 01:48:40 +08:00
from .const import (
DATA_CONNECTIONS,
2019-04-14 01:48:40 +08:00
SIGNAL_WEBSOCKET_CONNECTED,
SIGNAL_WEBSOCKET_DISCONNECTED,
)
2019-03-23 02:59:10 +08:00
2019-09-29 20:07:49 +03:00
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
2019-03-23 02:59:10 +08:00
"""Set up the API streams platform."""
entity = APICount()
async_add_entities([entity])
class APICount(SensorEntity):
2019-03-23 02:59:10 +08:00
"""Entity to represent how many people are connected to the stream API."""
_attr_name = "Connected clients"
_attr_native_unit_of_measurement = "clients"
def __init__(self) -> None:
2019-03-23 02:59:10 +08:00
"""Initialize the API count."""
self._attr_native_value = 0
2019-04-14 01:48:40 +08:00
async def async_added_to_hass(self) -> None:
2023-09-12 23:22:10 +03:00
"""Handle addition to hass."""
self.async_on_remove(
async_dispatcher_connect(
self.hass, SIGNAL_WEBSOCKET_CONNECTED, self._update_count
)
2019-04-14 01:48:40 +08:00
)
self.async_on_remove(
async_dispatcher_connect(
self.hass, SIGNAL_WEBSOCKET_DISCONNECTED, self._update_count
)
2019-04-14 01:48:40 +08:00
)
2019-03-23 02:59:10 +08:00
@callback
def _update_count(self) -> None:
self._attr_native_value = self.hass.data.get(DATA_CONNECTIONS, 0)
2020-04-01 14:19:51 -07:00
self.async_write_ha_state()