Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
8 changed files
with
142 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Test cases for the API stream sensor.""" | ||
|
||
from homeassistant.bootstrap import async_setup_component | ||
|
||
from tests.common import assert_setup_component | ||
from .test_auth import test_auth_via_msg | ||
|
||
|
||
async def test_websocket_api(hass, no_auth_websocket_client, legacy_auth): | ||
"""Test API streams.""" | ||
with assert_setup_component(1): | ||
await async_setup_component(hass, 'sensor', { | ||
'sensor': { | ||
'platform': 'websocket_api', | ||
} | ||
}) | ||
|
||
state = hass.states.get('sensor.connected_clients') | ||
assert state.state == '0' | ||
|
||
await test_auth_via_msg(no_auth_websocket_client, legacy_auth) | ||
|
||
state = hass.states.get('sensor.connected_clients') | ||
assert state.state == '1' | ||
|
||
await no_auth_websocket_client.close() | ||
await hass.async_block_till_done() | ||
|
||
state = hass.states.get('sensor.connected_clients') | ||
assert state.state == '0' |