Have api_streams sensor also monitor websocket connections (#4668)
This commit is contained in:
parent
84c89686a9
commit
4874030b70
2 changed files with 68 additions and 8 deletions
|
@ -2,9 +2,15 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||||
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
|
|
||||||
|
NAME_WS = 'homeassistant.components.websocket_api'
|
||||||
|
NAME_STREAM = 'homeassistant.components.api'
|
||||||
|
|
||||||
|
|
||||||
class StreamHandler(logging.Handler):
|
class StreamHandler(logging.Handler):
|
||||||
"""Check log messages for stream connect/disconnect."""
|
"""Check log messages for stream connect/disconnect."""
|
||||||
|
|
||||||
|
@ -16,13 +22,24 @@ class StreamHandler(logging.Handler):
|
||||||
|
|
||||||
def handle(self, record):
|
def handle(self, record):
|
||||||
"""Handle a log message."""
|
"""Handle a log message."""
|
||||||
if not record.msg.startswith('STREAM'):
|
if record.name == NAME_STREAM:
|
||||||
return
|
if not record.msg.startswith('STREAM'):
|
||||||
|
return
|
||||||
|
|
||||||
if record.msg.endswith('ATTACHED'):
|
if record.msg.endswith('ATTACHED'):
|
||||||
self.entity.count += 1
|
self.entity.count += 1
|
||||||
elif record.msg.endswith('RESPONSE CLOSED'):
|
elif record.msg.endswith('RESPONSE CLOSED'):
|
||||||
self.entity.count -= 1
|
self.entity.count -= 1
|
||||||
|
|
||||||
|
else:
|
||||||
|
if not record.msg.startswith('WS'):
|
||||||
|
return
|
||||||
|
elif len(record.args) < 2:
|
||||||
|
return
|
||||||
|
elif record.args[1] == 'Connected':
|
||||||
|
self.entity.count += 1
|
||||||
|
elif record.args[1] == 'Closed connection':
|
||||||
|
self.entity.count -= 1
|
||||||
|
|
||||||
self.entity.schedule_update_ha_state()
|
self.entity.schedule_update_ha_state()
|
||||||
|
|
||||||
|
@ -31,9 +48,18 @@ class StreamHandler(logging.Handler):
|
||||||
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||||
"""Set up the logger for filters."""
|
"""Set up the logger for filters."""
|
||||||
entity = APICount()
|
entity = APICount()
|
||||||
|
handler = StreamHandler(entity)
|
||||||
|
|
||||||
logging.getLogger('homeassistant.components.api').addHandler(
|
logging.getLogger(NAME_STREAM).addHandler(handler)
|
||||||
StreamHandler(entity))
|
logging.getLogger(NAME_WS).addHandler(handler)
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def remove_logger(event):
|
||||||
|
"""Remove our handlers."""
|
||||||
|
logging.getLogger(NAME_STREAM).removeHandler(handler)
|
||||||
|
logging.getLogger(NAME_WS).removeHandler(handler)
|
||||||
|
|
||||||
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, remove_logger)
|
||||||
|
|
||||||
yield from async_add_devices([entity])
|
yield from async_add_devices([entity])
|
||||||
|
|
||||||
|
|
|
@ -37,3 +37,37 @@ def test_api_streams(hass):
|
||||||
|
|
||||||
state = hass.states.get('sensor.connected_clients')
|
state = hass.states.get('sensor.connected_clients')
|
||||||
assert state.state == '1'
|
assert state.state == '1'
|
||||||
|
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def test_websocket_api(hass):
|
||||||
|
"""Test API streams."""
|
||||||
|
log = logging.getLogger('homeassistant.components.websocket_api')
|
||||||
|
|
||||||
|
with assert_setup_component(1):
|
||||||
|
yield from async_setup_component(hass, 'sensor', {
|
||||||
|
'sensor': {
|
||||||
|
'platform': 'api_streams',
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
state = hass.states.get('sensor.connected_clients')
|
||||||
|
assert state.state == '0'
|
||||||
|
|
||||||
|
log.debug('WS %s: %s', id(log), 'Connected')
|
||||||
|
yield from hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get('sensor.connected_clients')
|
||||||
|
assert state.state == '1'
|
||||||
|
|
||||||
|
log.debug('WS %s: %s', id(log), 'Connected')
|
||||||
|
yield from hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get('sensor.connected_clients')
|
||||||
|
assert state.state == '2'
|
||||||
|
|
||||||
|
log.debug('WS %s: %s', id(log), 'Closed connection')
|
||||||
|
yield from hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get('sensor.connected_clients')
|
||||||
|
assert state.state == '1'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue