3939CONF_ENTITY_PREFIX = 'entity_prefix'
4040CONF_FILTER = 'filter'
4141
42+ STATE_INIT = 'initializing'
43+ STATE_CONNECTING = 'connecting'
44+ STATE_CONNECTED = 'connected'
45+ STATE_RECONNECTING = 'reconnecting'
46+ STATE_DISCONNECTED = 'disconnected'
47+
4248DOMAIN = 'remote_homeassistant'
4349
4450DEFAULT_SUBSCRIBED_EVENTS = [EVENT_STATE_CHANGED ,
@@ -140,11 +146,29 @@ def __init__(self, hass, conf):
140146 self ._subscribe_events = conf .get (CONF_SUBSCRIBE_EVENTS )
141147 self ._entity_prefix = conf .get (CONF_ENTITY_PREFIX )
142148
149+ self ._connection_state_entity = 'sensor.'
150+
151+ if self ._entity_prefix != '' :
152+ self ._connection_state_entity = '{}{}' .format (self ._connection_state_entity , self ._entity_prefix )
153+
154+ self ._connection_state_entity = '{}remote_connection_{}_{}' .format (self ._connection_state_entity , self ._host .replace ('.' , '_' ).replace ('-' , '_' ), self ._port )
155+
143156 self ._connection = None
144157 self ._entities = set ()
145158 self ._handlers = {}
146159 self ._remove_listener = None
147160
161+ self ._instance_attrs = {
162+ 'host' : self ._host ,
163+ 'port' : self ._port ,
164+ 'secure' : self ._secure ,
165+ 'verify_ssl' : self ._verify_ssl ,
166+ 'entity_prefix' : self ._entity_prefix
167+ }
168+
169+ self ._entities .add (self ._connection_state_entity )
170+ self ._hass .states .async_set (self ._connection_state_entity , STATE_CONNECTING , self ._instance_attrs )
171+
148172 self .__id = 1
149173
150174 def _prefixed_entity_id (self , entity_id ):
@@ -166,6 +190,7 @@ async def async_connect(self):
166190 url = self ._get_url ()
167191
168192 session = async_get_clientsession (self ._hass , self ._verify_ssl )
193+ self ._hass .states .async_set (self ._connection_state_entity , STATE_CONNECTING , self ._instance_attrs )
169194
170195 while True :
171196 try :
@@ -174,17 +199,20 @@ async def async_connect(self):
174199 except aiohttp .client_exceptions .ClientError as err :
175200 _LOGGER .error (
176201 'Could not connect to %s, retry in 10 seconds...' , url )
202+ self ._hass .states .async_set (self ._connection_state_entity , STATE_RECONNECTING , self ._instance_attrs )
177203 await asyncio .sleep (10 )
178204 else :
179205 _LOGGER .info (
180206 'Connected to home-assistant websocket at %s' , url )
207+ self ._hass .states .async_set (self ._connection_state_entity , STATE_CONNECTED , self ._instance_attrs )
181208 break
182209
183210 async def stop ():
184211 """Close connection."""
185212 if self ._connection is not None :
186213 await self ._connection .close ()
187-
214+ self ._hass .states .async_set (self ._connection_state_entity , STATE_DISCONNECTED )
215+
188216 self ._hass .bus .async_listen_once (EVENT_HOMEASSISTANT_STOP , stop )
189217
190218 asyncio .ensure_future (self ._recv ())
@@ -210,6 +238,8 @@ async def _disconnected(self):
210238 self ._hass .states .async_remove (entity )
211239 if self ._remove_listener is not None :
212240 self ._remove_listener ()
241+
242+ self ._hass .states .async_set (self ._connection_state_entity , STATE_DISCONNECTED )
213243 self ._remove_listener = None
214244 self ._entities = set ()
215245 asyncio .ensure_future (self .async_connect ())
0 commit comments