mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 10:47:10 +00:00
Avoid creating battery sensor if Shelly device is external powered (#43243)
This commit is contained in:
parent
60314ecc61
commit
4c2bf1ddf5
@ -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
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user