Avoid creating battery sensor if Shelly device is external powered (#43243)

This commit is contained in:
Simone Chemelli 2020-11-16 11:49:23 +01:00 committed by GitHub
parent 60314ecc61
commit 4c2bf1ddf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,6 @@
"""Sensor for Shelly.""" """Sensor for Shelly."""
import logging
from homeassistant.components import sensor from homeassistant.components import sensor
from homeassistant.const import ( from homeassistant.const import (
CONCENTRATION_PARTS_PER_MILLION, CONCENTRATION_PARTS_PER_MILLION,
@ -12,7 +14,8 @@ from homeassistant.const import (
VOLT, VOLT,
) )
from .const import SHAIR_MAX_WORK_HOURS from . import ShellyDeviceWrapper, get_device_name
from .const import DATA_CONFIG_ENTRY, DOMAIN, REST, SHAIR_MAX_WORK_HOURS
from .entity import ( from .entity import (
BlockAttributeDescription, BlockAttributeDescription,
RestAttributeDescription, RestAttributeDescription,
@ -21,12 +24,17 @@ from .entity import (
async_setup_entry_attribute_entities, async_setup_entry_attribute_entities,
async_setup_entry_rest, async_setup_entry_rest,
) )
from .utils import temperature_unit from .utils import async_remove_entity_by_domain, temperature_unit
SENSORS = { _LOGGER = logging.getLogger(__name__)
BATTERY_SENSOR = {
("device", "battery"): BlockAttributeDescription( ("device", "battery"): BlockAttributeDescription(
name="Battery", unit=PERCENTAGE, device_class=sensor.DEVICE_CLASS_BATTERY name="Battery", unit=PERCENTAGE, device_class=sensor.DEVICE_CLASS_BATTERY
), ),
}
SENSORS = {
("device", "deviceTemp"): BlockAttributeDescription( ("device", "deviceTemp"): BlockAttributeDescription(
name="Device Temperature", name="Device Temperature",
unit=temperature_unit, unit=temperature_unit,
@ -175,6 +183,28 @@ REST_SENSORS = {
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up sensors for device.""" """Set up sensors for device."""
wrapper: ShellyDeviceWrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][
config_entry.entry_id
][REST]
if (
"external_power" in wrapper.device.settings
and wrapper.device.settings["external_power"] == 1
):
_LOGGER.debug(
"Removed battery sensor [externally powered] for %s",
get_device_name(wrapper.device),
)
unique_id = f'{wrapper.device.shelly["mac"]}-battery'
await async_remove_entity_by_domain(
hass, "sensor", unique_id, config_entry.entry_id
)
else:
await async_setup_entry_attribute_entities(
hass, config_entry, async_add_entities, BATTERY_SENSOR, ShellySensor
)
await async_setup_entry_attribute_entities( await async_setup_entry_attribute_entities(
hass, config_entry, async_add_entities, SENSORS, ShellySensor hass, config_entry, async_add_entities, SENSORS, ShellySensor
) )