3737CONF_API_PASSWORD = 'api_password'
3838CONF_SUBSCRIBE_EVENTS = 'subscribe_events'
3939CONF_ENTITY_PREFIX = 'entity_prefix'
40+ CONF_STATE_ENTITY_NAME = 'state_entity_name'
4041CONF_FILTER = 'filter'
4142
43+ STATE_INIT = 'initializing'
44+ STATE_CONNECTING = 'connecting'
45+ STATE_CONNECTED = 'connected'
46+ STATE_RECONNECTING = 'reconnecting'
47+ STATE_DISCONNECTED = 'disconnected'
48+
4249DOMAIN = 'remote_homeassistant'
4350
4451DEFAULT_SUBSCRIBED_EVENTS = [EVENT_STATE_CHANGED ,
4754
4855INSTANCES_SCHEMA = vol .Schema ({
4956 vol .Required (CONF_HOST ): cv .string ,
57+ vol .Required (CONF_STATE_ENTITY_NAME ): cv .string ,
5058 vol .Optional (CONF_PORT , default = 8123 ): cv .port ,
5159 vol .Optional (CONF_SECURE , default = False ): cv .boolean ,
5260 vol .Optional (CONF_VERIFY_SSL , default = True ): cv .boolean ,
@@ -112,6 +120,7 @@ def __init__(self, hass, conf):
112120 """Initialize the connection."""
113121 self ._hass = hass
114122 self ._host = conf .get (CONF_HOST )
123+ self ._connection_state_entity = 'sensor.' + conf .get (CONF_STATE_ENTITY_NAME )
115124 self ._port = conf .get (CONF_PORT )
116125 self ._secure = conf .get (CONF_SECURE )
117126 self ._verify_ssl = conf .get (CONF_VERIFY_SSL )
@@ -145,6 +154,17 @@ def __init__(self, hass, conf):
145154 self ._handlers = {}
146155 self ._remove_listener = None
147156
157+ self ._instance_attrs = {
158+ 'host' : self ._host ,
159+ 'port' : self ._port ,
160+ 'secure' : self ._secure ,
161+ 'verify_ssl' : self ._verify_ssl ,
162+ 'entity_prefix' : self ._entity_prefix
163+ }
164+
165+ self ._entities .add (self ._connection_state_entity )
166+ self ._hass .states .async_set (self ._connection_state_entity , STATE_CONNECTING , self ._instance_attrs )
167+
148168 self .__id = 1
149169
150170 def _prefixed_entity_id (self , entity_id ):
@@ -166,6 +186,7 @@ async def async_connect(self):
166186 url = self ._get_url ()
167187
168188 session = async_get_clientsession (self ._hass , self ._verify_ssl )
189+ self ._hass .states .async_set (self ._connection_state_entity , STATE_CONNECTING , self ._instance_attrs )
169190
170191 while True :
171192 try :
@@ -174,17 +195,20 @@ async def async_connect(self):
174195 except aiohttp .client_exceptions .ClientError as err :
175196 _LOGGER .error (
176197 'Could not connect to %s, retry in 10 seconds...' , url )
198+ self ._hass .states .async_set (self ._connection_state_entity , STATE_RECONNECTING , self ._instance_attrs )
177199 await asyncio .sleep (10 )
178200 else :
179201 _LOGGER .info (
180202 'Connected to home-assistant websocket at %s' , url )
203+ self ._hass .states .async_set (self ._connection_state_entity , STATE_CONNECTED , self ._instance_attrs )
181204 break
182205
183206 async def stop ():
184207 """Close connection."""
185208 if self ._connection is not None :
186209 await self ._connection .close ()
187-
210+ self ._hass .states .async_set (self ._connection_state_entity , STATE_DISCONNECTED )
211+
188212 self ._hass .bus .async_listen_once (EVENT_HOMEASSISTANT_STOP , stop )
189213
190214 asyncio .ensure_future (self ._recv ())
@@ -210,6 +234,8 @@ async def _disconnected(self):
210234 self ._hass .states .async_remove (entity )
211235 if self ._remove_listener is not None :
212236 self ._remove_listener ()
237+ if self ._connection_state_entity is not None :
238+ self ._hass .states .async_set (self ._connection_state_entity , STATE_DISCONNECTED )
213239 self ._remove_listener = None
214240 self ._entities = set ()
215241 asyncio .ensure_future (self .async_connect ())
0 commit comments