-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from vincentwolsink/natural_mode_energy_saving
Add natural mode and energy saving for Piazetta stoves (and maybe others)
- Loading branch information
Showing
5 changed files
with
202 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from homeassistant.helpers.update_coordinator import ( | ||
CoordinatorEntity, | ||
DataUpdateCoordinator, | ||
) | ||
from homeassistant.components.number import NumberEntity | ||
from homeassistant.helpers.entity import DeviceInfo | ||
from .const import NUMBERS, DOMAIN | ||
|
||
|
||
async def async_setup_entry(hass, entry, async_add_entities): | ||
coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][ | ||
"coordinator" | ||
] | ||
agua = hass.data[DOMAIN][entry.entry_id]["agua"] | ||
|
||
numbers = [] | ||
for device in agua.devices: | ||
for number in NUMBERS: | ||
if getattr(device, number.key, None) is not None: | ||
numbers.append(AguaIOTHeatingNumber(coordinator, device, number)) | ||
|
||
async_add_entities(numbers, True) | ||
|
||
|
||
class AguaIOTHeatingNumber(CoordinatorEntity, NumberEntity): | ||
def __init__(self, coordinator, device, description): | ||
"""Initialize the thermostat.""" | ||
CoordinatorEntity.__init__(self, coordinator) | ||
self._device = device | ||
self.entity_description = description | ||
|
||
@property | ||
def unique_id(self): | ||
"""Return a unique ID.""" | ||
return f"{self._device.id_device}_{self.entity_description.key}" | ||
|
||
@property | ||
def name(self): | ||
"""Return the name of the device, if any.""" | ||
return f"{self._device.name} {self.entity_description.name}" | ||
|
||
@property | ||
def device_info(self): | ||
"""Return the device info.""" | ||
return DeviceInfo( | ||
identifiers={(DOMAIN, self._device.id_device)}, | ||
name=self._device.name, | ||
manufacturer="Micronova", | ||
model=self._device.name_product, | ||
) | ||
|
||
@property | ||
def native_value(self): | ||
"""Return the state of the sensor.""" | ||
return getattr(self._device, self.entity_description.key) | ||
|
||
@property | ||
def native_min_value(self): | ||
return getattr(self._device, f"min_{self.entity_description.key}") | ||
|
||
@property | ||
def native_max_value(self): | ||
return getattr(self._device, f"max_{self.entity_description.key}") | ||
|
||
async def async_set_native_value(self, value): | ||
try: | ||
await self.hass.async_add_executor_job( | ||
setattr, | ||
self._device, | ||
self.entity_description.key, | ||
value, | ||
) | ||
await self.coordinator.async_request_refresh() | ||
except (ValueError, AguaIOTError) as err: | ||
_LOGGER.error("Failed to set value, error: %s", err) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from homeassistant.helpers.update_coordinator import ( | ||
CoordinatorEntity, | ||
DataUpdateCoordinator, | ||
) | ||
from homeassistant.components.switch import SwitchEntity | ||
from homeassistant.helpers.entity import DeviceInfo | ||
from .const import SWITCHES, DOMAIN | ||
from py_agua_iot import Error as AguaIOTError | ||
|
||
|
||
async def async_setup_entry(hass, entry, async_add_entities): | ||
coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][ | ||
"coordinator" | ||
] | ||
agua = hass.data[DOMAIN][entry.entry_id]["agua"] | ||
|
||
switches = [] | ||
for device in agua.devices: | ||
for switch in SWITCHES: | ||
if getattr(device, switch.key, None) is not None: | ||
switches.append(AguaIOTHeatingSwitch(coordinator, device, switch)) | ||
|
||
async_add_entities(switches, True) | ||
|
||
|
||
class AguaIOTHeatingSwitch(CoordinatorEntity, SwitchEntity): | ||
def __init__(self, coordinator, device, description): | ||
"""Initialize the thermostat.""" | ||
CoordinatorEntity.__init__(self, coordinator) | ||
self._device = device | ||
self.entity_description = description | ||
|
||
@property | ||
def unique_id(self): | ||
"""Return a unique ID.""" | ||
return f"{self._device.id_device}_{self.entity_description.key}" | ||
|
||
@property | ||
def name(self): | ||
"""Return the name of the device, if any.""" | ||
return f"{self._device.name} {self.entity_description.name}" | ||
|
||
@property | ||
def device_info(self): | ||
"""Return the device info.""" | ||
return DeviceInfo( | ||
identifiers={(DOMAIN, self._device.id_device)}, | ||
name=self._device.name, | ||
manufacturer="Micronova", | ||
model=self._device.name_product, | ||
) | ||
|
||
@property | ||
def is_on(self): | ||
"""Return the state of the sensor.""" | ||
return bool(getattr(self._device, self.entity_description.key)) | ||
|
||
async def async_turn_off(self): | ||
"""Turn device off.""" | ||
try: | ||
await self.hass.async_add_executor_job( | ||
setattr(self._device, self.entity_description.key, False) | ||
) | ||
await self.coordinator.async_request_refresh() | ||
except AguaIOTError as err: | ||
_LOGGER.error( | ||
"Failed to turn off '%s', error: %s", | ||
f"{self._device.name} {self.entity_description.name}", | ||
err, | ||
) | ||
|
||
async def async_turn_on(self): | ||
"""Turn device on.""" | ||
try: | ||
await self.hass.async_add_executor_job( | ||
setattr(self._device, self.entity_description.key, True) | ||
) | ||
await self.coordinator.async_request_refresh() | ||
except AguaIOTError as err: | ||
_LOGGER.error( | ||
"Failed to turn on '%s', error: %s", | ||
f"{self._device.name} {self.entity_description.name}", | ||
err, | ||
) |