From e581d41ded30aac220875c4e5f8fd41e56889742 Mon Sep 17 00:00:00 2001 From: Eliseo Martelli Date: Tue, 5 Feb 2019 13:42:14 +0100 Subject: [PATCH] Fix googlehome alarm sensor platform (#20742) * fixed googlehome alarm sensor platform * removed info and moved info discovery out of loop * moved device info up --- homeassistant/components/googlehome/sensor.py | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/googlehome/sensor.py b/homeassistant/components/googlehome/sensor.py index f2a0f822dbf..90b9cda80bb 100644 --- a/homeassistant/components/googlehome/sensor.py +++ b/homeassistant/components/googlehome/sensor.py @@ -7,11 +7,10 @@ https://home-assistant.io/components/sensor.googlehome/ import logging from datetime import timedelta -from homeassistant.components.sensor import ENTITY_ID_FORMAT from homeassistant.components.googlehome import ( CLIENT, DOMAIN as GOOGLEHOME_DOMAIN, NAME) from homeassistant.const import DEVICE_CLASS_TIMESTAMP -from homeassistant.helpers.entity import Entity, async_generate_entity_id +from homeassistant.helpers.entity import Entity import homeassistant.util.dt as dt_util @@ -35,11 +34,16 @@ async def async_setup_platform(hass, config, if discovery_info is None: _LOGGER.warning( "To use this you need to configure the 'googlehome' component") + return + + await hass.data[CLIENT].update_info(discovery_info['host']) + data = hass.data[GOOGLEHOME_DOMAIN][discovery_info['host']] + info = data.get('info', {}) devices = [] for condition in SENSOR_TYPES: device = GoogleHomeAlarm(hass.data[CLIENT], condition, - discovery_info) + discovery_info, info.get('name', NAME)) devices.append(device) async_add_entities(devices, True) @@ -48,7 +52,7 @@ async def async_setup_platform(hass, config, class GoogleHomeAlarm(Entity): """Representation of a GoogleHomeAlarm.""" - def __init__(self, client, condition, config): + def __init__(self, client, condition, config, name): """Initialize the GoogleHomeAlarm sensor.""" self._host = config['host'] self._client = client @@ -56,18 +60,7 @@ class GoogleHomeAlarm(Entity): self._name = None self._state = None self._available = True - - async def async_added_to_hass(self): - """Subscribe GoogleHome events.""" - await self._client.update_info(self._host) - data = self.hass.data[GOOGLEHOME_DOMAIN][self._host] - info = data.get('info', {}) - if info is None: - return - self._name = "{} {}".format(info.get('name', NAME), - SENSOR_TYPES[self._condition]) - self.entity_id = async_generate_entity_id( - ENTITY_ID_FORMAT, self._name, hass=self.hass) + self._name = "{} {}".format(name, SENSOR_TYPES[self._condition]) async def async_update(self): """Update the data."""