diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index 5d29aae8d5e..81dc2ef1c11 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -1,4 +1,6 @@ """Sensor for Shelly.""" +import logging + from homeassistant.components import sensor from homeassistant.const import ( CONCENTRATION_PARTS_PER_MILLION, @@ -12,7 +14,8 @@ from homeassistant.const import ( 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 ( BlockAttributeDescription, RestAttributeDescription, @@ -21,12 +24,17 @@ from .entity import ( async_setup_entry_attribute_entities, 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( name="Battery", unit=PERCENTAGE, device_class=sensor.DEVICE_CLASS_BATTERY ), +} + +SENSORS = { ("device", "deviceTemp"): BlockAttributeDescription( name="Device Temperature", unit=temperature_unit, @@ -175,6 +183,28 @@ REST_SENSORS = { async def async_setup_entry(hass, config_entry, async_add_entities): """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( hass, config_entry, async_add_entities, SENSORS, ShellySensor )