From 12caf17848a884d6decdf91c7882514d468d31a3 Mon Sep 17 00:00:00 2001 From: mlemainque Date: Fri, 8 May 2020 19:21:24 +0200 Subject: [PATCH] Add Daikin total/cool/heat power sensors (#34391) --- homeassistant/components/daikin/__init__.py | 2 +- homeassistant/components/daikin/const.py | 42 +++++++++- homeassistant/components/daikin/sensor.py | 91 +++++++++++++++++---- homeassistant/const.py | 1 + 4 files changed, 115 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/daikin/__init__.py b/homeassistant/components/daikin/__init__.py index 5c461075f85..1e03938dfcf 100644 --- a/homeassistant/components/daikin/__init__.py +++ b/homeassistant/components/daikin/__init__.py @@ -119,7 +119,7 @@ async def daikin_api_setup(hass, host, key, uuid, password): class DaikinApi: """Keep the Daikin instance in one place and centralize the update.""" - def __init__(self, device): + def __init__(self, device: Appliance): """Initialize the Daikin Handle.""" self.device = device self.name = device.values.get("name", "Daikin AC") diff --git a/homeassistant/components/daikin/const.py b/homeassistant/components/daikin/const.py index 452331ff734..30d34b898d3 100644 --- a/homeassistant/components/daikin/const.py +++ b/homeassistant/components/daikin/const.py @@ -1,25 +1,61 @@ """Constants for Daikin.""" -from homeassistant.const import CONF_ICON, CONF_NAME, CONF_TYPE +from homeassistant.const import ( + CONF_DEVICE_CLASS, + CONF_ICON, + CONF_NAME, + CONF_TYPE, + CONF_UNIT_OF_MEASUREMENT, + DEVICE_CLASS_POWER, + DEVICE_CLASS_TEMPERATURE, + ENERGY_KILO_WATT_HOUR, + POWER_KILO_WATT, + TEMP_CELSIUS, +) ATTR_TARGET_TEMPERATURE = "target_temperature" ATTR_INSIDE_TEMPERATURE = "inside_temperature" ATTR_OUTSIDE_TEMPERATURE = "outside_temperature" +ATTR_TOTAL_POWER = "total_power" +ATTR_COOL_ENERGY = "cool_energy" +ATTR_HEAT_ENERGY = "heat_energy" ATTR_STATE_ON = "on" ATTR_STATE_OFF = "off" SENSOR_TYPE_TEMPERATURE = "temperature" +SENSOR_TYPE_POWER = "power" +SENSOR_TYPE_ENERGY = "energy" SENSOR_TYPES = { ATTR_INSIDE_TEMPERATURE: { CONF_NAME: "Inside Temperature", - CONF_ICON: "mdi:thermometer", CONF_TYPE: SENSOR_TYPE_TEMPERATURE, + CONF_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE, + CONF_UNIT_OF_MEASUREMENT: TEMP_CELSIUS, }, ATTR_OUTSIDE_TEMPERATURE: { CONF_NAME: "Outside Temperature", - CONF_ICON: "mdi:thermometer", CONF_TYPE: SENSOR_TYPE_TEMPERATURE, + CONF_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE, + CONF_UNIT_OF_MEASUREMENT: TEMP_CELSIUS, + }, + ATTR_TOTAL_POWER: { + CONF_NAME: "Total Power Consumption", + CONF_TYPE: SENSOR_TYPE_POWER, + CONF_DEVICE_CLASS: DEVICE_CLASS_POWER, + CONF_UNIT_OF_MEASUREMENT: POWER_KILO_WATT, + }, + ATTR_COOL_ENERGY: { + CONF_NAME: "Cool Energy Consumption", + CONF_TYPE: SENSOR_TYPE_ENERGY, + CONF_ICON: "mdi:snowflake", + CONF_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR, + }, + ATTR_HEAT_ENERGY: { + CONF_NAME: "Heat Energy Consumption", + CONF_TYPE: SENSOR_TYPE_ENERGY, + CONF_ICON: "mdi:fire", + CONF_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR, }, } diff --git a/homeassistant/components/daikin/sensor.py b/homeassistant/components/daikin/sensor.py index c389566396d..02cb8a0d3cd 100644 --- a/homeassistant/components/daikin/sensor.py +++ b/homeassistant/components/daikin/sensor.py @@ -1,11 +1,26 @@ """Support for Daikin AC sensors.""" import logging -from homeassistant.const import CONF_ICON, CONF_NAME, TEMP_CELSIUS +from homeassistant.const import ( + CONF_DEVICE_CLASS, + CONF_ICON, + CONF_NAME, + CONF_TYPE, + CONF_UNIT_OF_MEASUREMENT, +) from homeassistant.helpers.entity import Entity -from . import DOMAIN as DAIKIN_DOMAIN -from .const import ATTR_INSIDE_TEMPERATURE, ATTR_OUTSIDE_TEMPERATURE, SENSOR_TYPES +from . import DOMAIN as DAIKIN_DOMAIN, DaikinApi +from .const import ( + ATTR_COOL_ENERGY, + ATTR_HEAT_ENERGY, + ATTR_INSIDE_TEMPERATURE, + ATTR_OUTSIDE_TEMPERATURE, + ATTR_TOTAL_POWER, + SENSOR_TYPE_POWER, + SENSOR_TYPE_TEMPERATURE, + SENSOR_TYPES, +) _LOGGER = logging.getLogger(__name__) @@ -24,13 +39,26 @@ async def async_setup_entry(hass, entry, async_add_entities): sensors = [ATTR_INSIDE_TEMPERATURE] if daikin_api.device.support_outside_temperature: sensors.append(ATTR_OUTSIDE_TEMPERATURE) - async_add_entities([DaikinClimateSensor(daikin_api, sensor) for sensor in sensors]) + if daikin_api.device.support_energy_consumption: + sensors.append(ATTR_TOTAL_POWER) + sensors.append(ATTR_COOL_ENERGY) + sensors.append(ATTR_HEAT_ENERGY) + async_add_entities([DaikinSensor.factory(daikin_api, sensor) for sensor in sensors]) -class DaikinClimateSensor(Entity): +class DaikinSensor(Entity): """Representation of a Sensor.""" - def __init__(self, api, monitored_state) -> None: + @staticmethod + def factory(api: DaikinApi, monitored_state: str): + """Initialize any DaikinSensor.""" + cls = { + SENSOR_TYPE_TEMPERATURE: DaikinClimateSensor, + SENSOR_TYPE_POWER: DaikinPowerSensor, + }[SENSOR_TYPES[monitored_state][CONF_TYPE]] + return cls(api, monitored_state) + + def __init__(self, api: DaikinApi, monitored_state: str) -> None: """Initialize the sensor.""" self._api = api self._sensor = SENSOR_TYPES[monitored_state] @@ -42,11 +70,6 @@ class DaikinClimateSensor(Entity): """Return a unique ID.""" return f"{self._api.device.mac}-{self._device_attribute}" - @property - def icon(self): - """Icon to use in the frontend, if any.""" - return self._sensor[CONF_ICON] - @property def name(self): """Return the name of the sensor.""" @@ -55,16 +78,22 @@ class DaikinClimateSensor(Entity): @property def state(self): """Return the state of the sensor.""" - if self._device_attribute == ATTR_INSIDE_TEMPERATURE: - return self._api.device.inside_temperature - if self._device_attribute == ATTR_OUTSIDE_TEMPERATURE: - return self._api.device.outside_temperature - return None + raise NotImplementedError + + @property + def device_class(self): + """Return the class of this device.""" + return self._sensor.get(CONF_DEVICE_CLASS) + + @property + def icon(self): + """Return the icon of this device.""" + return self._sensor.get(CONF_ICON) @property def unit_of_measurement(self): """Return the unit of measurement.""" - return TEMP_CELSIUS + return self._sensor[CONF_UNIT_OF_MEASUREMENT] async def async_update(self): """Retrieve latest state.""" @@ -74,3 +103,31 @@ class DaikinClimateSensor(Entity): def device_info(self): """Return a device description for device registry.""" return self._api.device_info + + +class DaikinClimateSensor(DaikinSensor): + """Representation of a Climate Sensor.""" + + @property + def state(self): + """Return the internal state of the sensor.""" + if self._device_attribute == ATTR_INSIDE_TEMPERATURE: + return self._api.device.inside_temperature + if self._device_attribute == ATTR_OUTSIDE_TEMPERATURE: + return self._api.device.outside_temperature + return None + + +class DaikinPowerSensor(DaikinSensor): + """Representation of a power/energy consumption sensor.""" + + @property + def state(self): + """Return the state of the sensor.""" + if self._device_attribute == ATTR_TOTAL_POWER: + return self._api.device.current_total_power_consumption + if self._device_attribute == ATTR_COOL_ENERGY: + return self._api.device.last_hour_cool_power_consumption + if self._device_attribute == ATTR_HEAT_ENERGY: + return self._api.device.last_hour_heat_power_consumption + return None diff --git a/homeassistant/const.py b/homeassistant/const.py index a59230d447a..ec8c29de0b7 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -348,6 +348,7 @@ ATTR_TEMPERATURE = "temperature" # #### UNITS OF MEASUREMENT #### # Power units POWER_WATT = "W" +POWER_KILO_WATT = f"k{POWER_WATT}" # Voltage units VOLT = "V"