* Add MELCloud integration * Provides a climate and sensor platforms. Multiple platforms on one go is not the best option, but it does not make sense to remove them and commit them later either. * Email and access token are stored to the ConfigEntry. The token can be updated by adding the integration again with the same email address. The config flow is aborted and the update is performed on the background. * Run isort * Fix pylint errors * Run black * Increase coverage * Update pymelcloud dependency * Add HVAC_MODE_OFF emulation * Remove print * Update pymelcloud to enable device type filtering * Collapse except blocks and chain ClientNotReadys * Add preliminary documentation URL * Use list comp for creating model info Filters out empty model names form units. * f-string galore Dropped 'HVAC' from AtaDevice name template. * Delegate fan mode mapping to pymelcloud * Fix type annotation * Access AtaDevice through self._device * Prefer list comprehension * Update pymelcloud to leverage device type grouping The updated backend lib returns devices in a dict grouped by the device type. The devices do not necessarily need to be in a single list and this way isinstance is not required to extract devices by type. * Remove DOMAIN presence check This does not seem to make much sense after all. * Fix async_setup_entry Entry setup used half-baked naming from few experimentations back. The naming conventiens were unified to match the platforms. A redundant noneness check was also removed after evaluating the possible return values from the backend lib. * Simplify empty model name check * Improve config validation * Use config_validation strings. * Add CONF_EMAIL to config schema. The value is not strictly required when configuring through configuration.yaml, but having it there makes things more consistent. * Use dict[key] to access required properties. * Add DOMAIN in config check back to async_setup. This is required if an integration is configured throught config_flow. * Remove unused manifest properties * Remove redundant ClimateDevice property override * Add __init__.py to coverage exclusion * Use CONF_USERNAME instead of CONF_EMAIL * Use asyncio.gather instead of asyncio.wait * Misc fixes * any -> Any * Better names for dict iterations * Proper dict access with mandatory/known keys * Remove unused 'name' argument * Remove unnecessary platform info from unique_ids * Remove redundant methods from climate platform * Remove redundant default value from dict get * Update ConfigFlow sub-classing * Define sensors in a dict instead of a list * Use _abort_if_unique_id_configured to update token * Fix them tests * Remove current state guards * Fix that gather call * Implement sensor definitions without str manipulation * Use relative intra-package imports * Update homeassistant/components/melcloud/config_flow.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
98 lines
2.9 KiB
Python
98 lines
2.9 KiB
Python
"""Support for MelCloud device sensors."""
|
|
import logging
|
|
|
|
from pymelcloud import DEVICE_TYPE_ATA, AtaDevice
|
|
|
|
from homeassistant.const import DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS
|
|
from homeassistant.helpers.entity import Entity
|
|
from homeassistant.util.unit_system import UnitSystem
|
|
|
|
from .const import DOMAIN, TEMP_UNIT_LOOKUP
|
|
|
|
ATTR_MEASUREMENT_NAME = "measurement_name"
|
|
ATTR_ICON = "icon"
|
|
ATTR_UNIT_FN = "unit_fn"
|
|
ATTR_DEVICE_CLASS = "device_class"
|
|
ATTR_VALUE_FN = "value_fn"
|
|
|
|
SENSORS = {
|
|
"room_temperature": {
|
|
ATTR_MEASUREMENT_NAME: "Room Temperature",
|
|
ATTR_ICON: "mdi:thermometer",
|
|
ATTR_UNIT_FN: lambda x: TEMP_UNIT_LOOKUP.get(x.device.temp_unit, TEMP_CELSIUS),
|
|
ATTR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE,
|
|
ATTR_VALUE_FN: lambda x: x.device.room_temperature,
|
|
},
|
|
"energy": {
|
|
ATTR_MEASUREMENT_NAME: "Energy",
|
|
ATTR_ICON: "mdi:factory",
|
|
ATTR_UNIT_FN: lambda x: "kWh",
|
|
ATTR_DEVICE_CLASS: None,
|
|
ATTR_VALUE_FN: lambda x: x.device.total_energy_consumed,
|
|
},
|
|
}
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
|
"""Set up MELCloud device sensors based on config_entry."""
|
|
mel_devices = hass.data[DOMAIN].get(entry.entry_id)
|
|
async_add_entities(
|
|
[
|
|
MelCloudSensor(mel_device, measurement, definition, hass.config.units)
|
|
for measurement, definition in SENSORS.items()
|
|
for mel_device in mel_devices[DEVICE_TYPE_ATA]
|
|
],
|
|
True,
|
|
)
|
|
|
|
|
|
class MelCloudSensor(Entity):
|
|
"""Representation of a Sensor."""
|
|
|
|
def __init__(self, device: AtaDevice, measurement, definition, units: UnitSystem):
|
|
"""Initialize the sensor."""
|
|
self._api = device
|
|
self._name_slug = device.name
|
|
self._measurement = measurement
|
|
self._def = definition
|
|
|
|
@property
|
|
def unique_id(self):
|
|
"""Return a unique ID."""
|
|
return f"{self._api.device.serial}-{self._api.device.mac}-{self._measurement}"
|
|
|
|
@property
|
|
def icon(self):
|
|
"""Return the icon to use in the frontend, if any."""
|
|
return self._def[ATTR_ICON]
|
|
|
|
@property
|
|
def name(self):
|
|
"""Return the name of the sensor."""
|
|
return f"{self._name_slug} {self._def[ATTR_MEASUREMENT_NAME]}"
|
|
|
|
@property
|
|
def state(self):
|
|
"""Return the state of the sensor."""
|
|
return self._def[ATTR_VALUE_FN](self._api)
|
|
|
|
@property
|
|
def unit_of_measurement(self):
|
|
"""Return the unit of measurement."""
|
|
return self._def[ATTR_UNIT_FN](self._api)
|
|
|
|
@property
|
|
def device_class(self):
|
|
"""Return device class."""
|
|
return self._def[ATTR_DEVICE_CLASS]
|
|
|
|
async def async_update(self):
|
|
"""Retrieve latest state."""
|
|
await self._api.async_update()
|
|
|
|
@property
|
|
def device_info(self):
|
|
"""Return a device description for device registry."""
|
|
return self._api.device_info
|