Propper disconnection

This commit is contained in:
Thomas Lovén 2019-06-27 10:29:40 +02:00
parent 1c2d92e0d2
commit fa2540095a

View File

@ -46,26 +46,37 @@ def handle_update(hass, connection, msg):
class BrowserModEntity(Entity): class BrowserModEntity(Entity):
def __init__(self, hass, deviceID, alias=None): def __init__(self, hass, deviceID, alias=None):
self.hass = hass self._hass = hass
self.deviceID = deviceID self._deviceID = deviceID
self.alias = alias self._alias = alias
self.ws_data = {} self._ws_data = {}
self._ws_connection = None
self.entity_id = async_generate_entity_id("media_player.{}", alias or deviceID, hass=hass) self.entity_id = async_generate_entity_id("media_player.{}", alias or deviceID, hass=hass)
def ws_send(self, command, data=None): def ws_send(self, command, data=None):
data = data or {} data = data or {}
self.ws_connection.send_message(event_message(self.ws_cid, { self._ws_connection.send_message(event_message(self._ws_cid, {
"command": command, "command": command,
**data, **data,
})) }))
def ws_connect(self, connection, cid): def ws_connect(self, connection, cid):
self.ws_cid = cid self._ws_cid = cid
self.ws_connection = connection self._ws_connection = connection
_LOGGER.error(f"Connecting {self.entity_id}") _LOGGER.error(f"Connecting {self.entity_id}")
self.ws_send("update") self.ws_send("update")
connection.subscriptions[cid] = self.ws_disconnect
self.schedule_update_ha_state()
def ws_disconnect(self):
self._ws_cid = None
self._ws_connection = None
self.schedule_update_ha_state()
def ws_update(self, data): def ws_update(self, data):
self.ws_data = data self._ws_data = data
self.schedule_update_ha_state() self.schedule_update_ha_state()
pass
@property
def device_id(self):
return self._deviceID