Shelly: minor improvements (#43138)

This commit is contained in:
Simone Chemelli 2020-11-12 19:49:06 +01:00 committed by GitHub
parent c620fa344e
commit 31502e960a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 6 deletions

View file

@ -54,6 +54,11 @@ async def get_coap_context(hass):
return context return context
def get_device_name(device):
"""Naming for device."""
return device.settings["name"] or device.settings["device"]["hostname"]
async def async_setup(hass: HomeAssistant, config: dict): async def async_setup(hass: HomeAssistant, config: dict):
"""Set up the Shelly component.""" """Set up the Shelly component."""
hass.data[DOMAIN] = {DATA_CONFIG_ENTRY: {}} hass.data[DOMAIN] = {DATA_CONFIG_ENTRY: {}}
@ -126,7 +131,7 @@ class ShellyDeviceWrapper(update_coordinator.DataUpdateCoordinator):
super().__init__( super().__init__(
hass, hass,
_LOGGER, _LOGGER,
name=device.settings["name"] or device.settings["device"]["hostname"], name=get_device_name(device),
update_interval=timedelta(seconds=update_interval), update_interval=timedelta(seconds=update_interval),
) )
self.hass = hass self.hass = hass
@ -186,7 +191,7 @@ class ShellyDeviceRestWrapper(update_coordinator.DataUpdateCoordinator):
super().__init__( super().__init__(
hass, hass,
_LOGGER, _LOGGER,
name=device.settings["name"] or device.settings["device"]["hostname"], name=get_device_name(device),
update_interval=timedelta(seconds=REST_SENSORS_UPDATE_INTERVAL), update_interval=timedelta(seconds=REST_SENSORS_UPDATE_INTERVAL),
) )
self.device = device self.device = device
@ -195,9 +200,7 @@ class ShellyDeviceRestWrapper(update_coordinator.DataUpdateCoordinator):
"""Fetch data.""" """Fetch data."""
try: try:
async with async_timeout.timeout(5): async with async_timeout.timeout(5):
_LOGGER.debug( _LOGGER.debug("REST update for %s", get_device_name(self.device))
"REST update for %s", self.device.settings["device"]["hostname"]
)
return await self.device.update_status() return await self.device.update_status()
except OSError as err: except OSError as err:
raise update_coordinator.UpdateFailed("Error fetching data") from err raise update_coordinator.UpdateFailed("Error fetching data") from err

View file

@ -124,6 +124,7 @@ SENSORS = {
name="Gas Concentration", name="Gas Concentration",
unit=CONCENTRATION_PARTS_PER_MILLION, unit=CONCENTRATION_PARTS_PER_MILLION,
value=lambda value: value, value=lambda value: value,
icon="mdi:gauge",
# "sensorOp" is "normal" when the Shelly Gas is working properly and taking measurements. # "sensorOp" is "normal" when the Shelly Gas is working properly and taking measurements.
available=lambda block: block.sensorOp == "normal", available=lambda block: block.sensorOp == "normal",
), ),
@ -144,7 +145,7 @@ SENSORS = {
unit=LIGHT_LUX, unit=LIGHT_LUX,
device_class=sensor.DEVICE_CLASS_ILLUMINANCE, device_class=sensor.DEVICE_CLASS_ILLUMINANCE,
), ),
("sensor", "tilt"): BlockAttributeDescription(name="tilt", unit=DEGREE), ("sensor", "tilt"): BlockAttributeDescription(name="Tilt", unit=DEGREE),
("relay", "totalWorkTime"): BlockAttributeDescription( ("relay", "totalWorkTime"): BlockAttributeDescription(
name="Lamp life", name="Lamp life",
unit=PERCENTAGE, unit=PERCENTAGE,