Skip to content

Commit

Permalink
Centralising common properties for all entites into a generic myhome_…
Browse files Browse the repository at this point in the history
…device
  • Loading branch information
anotherjulien committed Sep 14, 2021
1 parent 458cc2e commit 271cec4
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 303 deletions.
89 changes: 35 additions & 54 deletions custom_components/myhome/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
CONF_NAME,
CONF_DEVICES,
CONF_ENTITIES,
STATE_ON,
)
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity

from OWNd.message import (
OWNDryContactEvent,
Expand All @@ -60,6 +62,7 @@
DOMAIN,
LOGGER,
)
from .myhome_device import MyHOMEEntity
from .gateway import MyHOMEGatewayHandler

MYHOME_SCHEMA = vol.Schema(
Expand Down Expand Up @@ -224,7 +227,7 @@ async def async_unload_entry(hass, config_entry): # pylint: disable=unused-argu
del hass.data[DOMAIN][CONF_ENTITIES][_binary_sensor]


class MyHOMEDryContact(BinarySensorEntity):
class MyHOMEDryContact(MyHOMEEntity, BinarySensorEntity):
def __init__(
self,
hass,
Expand All @@ -238,44 +241,26 @@ def __init__(
model: str,
gateway: MyHOMEGatewayHandler,
):
super().__init__(
hass=hass,
name=name,
device_id=device_id,
who=who,
where=where,
manufacturer=manufacturer,
model=model,
gateway=gateway,
)

self._hass = hass
self._device_id = device_id
self._where = where
self._manufacturer = manufacturer or "BTicino S.p.A."
self._who = who
self._model = model
self._gateway_handler = gateway
self._inverted = inverted

self._attr_name = name
self._attr_unique_id = self._device_id

self._attr_device_info = {
"identifiers": {(DOMAIN, self._device_id)},
"name": self._attr_name,
"manufacturer": self._manufacturer,
"model": self._model,
"via_device": (DOMAIN, self._gateway_handler.unique_id),
}

self._attr_device_class = device_class
self._attr_entity_registry_enabled_default = True
self._attr_should_poll = False

self._attr_is_on = False
self._attr_extra_state_attributes = {
"Sensor": f"({self._where[0]}){self._where[1:]}"
}

async def async_added_to_hass(self):
"""When entity is added to hass."""
self._hass.data[DOMAIN][CONF_ENTITIES][self._attr_unique_id] = self
await self.async_update()

async def async_will_remove_from_hass(self):
"""When entity is removed from hass."""
del self._hass.data[DOMAIN][CONF_ENTITIES][self._attr_unique_id]

async def async_update(self):
"""Update the entity.
Expand All @@ -292,7 +277,7 @@ def handle_event(self, message: OWNDryContactEvent):
self.async_schedule_update_ha_state()


class MyHOMEMotionSensor(BinarySensorEntity):
class MyHOMEMotionSensor(MyHOMEEntity, BinarySensorEntity, RestoreEntity):
def __init__(
self,
hass,
Expand All @@ -307,35 +292,29 @@ def __init__(
model: str,
gateway: MyHOMEGatewayHandler,
):
super().__init__(
hass=hass,
name=name,
device_id=device_id,
who=who,
where=where,
manufacturer=manufacturer,
model=model,
gateway=gateway,
)

self._hass = hass
self._device_id = device_id
self._entity_name = entity_name
self._where = where
self._manufacturer = manufacturer or "BTicino S.p.A."
self._who = who
self._model = model
self._gateway_handler = gateway
self._inverted = inverted

self._attr_name = name
self._attr_unique_id = f"{self._device_id}-{self._entity_name}"

self._attr_force_update = False
self._last_updated = None
self._timeout = timedelta(seconds=315)

self._attr_device_info = {
"identifiers": {(DOMAIN, self._device_id)},
"name": self._attr_name,
"manufacturer": self._manufacturer,
"model": self._model,
"via_device": (DOMAIN, self._gateway_handler.unique_id),
}

self._attr_device_class = device_class
self._attr_entity_registry_enabled_default = True
self._attr_should_poll = True
self._attr_is_on = False
self._attr_is_on = None
self._attr_extra_state_attributes = {
"A": where[: len(where) // 2],
"PL": where[len(where) // 2 :],
Expand All @@ -352,12 +331,12 @@ async def async_added_to_hass(self):
await self._gateway_handler.send_status_request(
OWNLightingCommand.get_motion_timeout(self._where)
)
state = await self.async_get_last_state()
if state:
self._attr_is_on = state.state == STATE_ON
self._last_updated = state.last_updated
await self.async_update()

async def async_will_remove_from_hass(self):
"""When entity is removed from hass."""
del self._hass.data[DOMAIN][CONF_ENTITIES][self._attr_unique_id]

async def async_update(self):
"""Update the entity.
Expand Down Expand Up @@ -385,4 +364,6 @@ def handle_event(self, message: OWNLightingEvent):
message.pir_sensitivity
]
self._last_updated = datetime.now(timezone.utc)
self.async_schedule_update_ha_state()
self._attr_force_update = True
self.async_write_ha_state()
self._attr_force_update = False
67 changes: 25 additions & 42 deletions custom_components/myhome/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
DOMAIN,
LOGGER,
)
from .myhome_device import MyHOMEEntity
from .gateway import MyHOMEGatewayHandler

MYHOME_SCHEMA = vol.Schema(
Expand Down Expand Up @@ -166,7 +167,7 @@ async def async_setup_entry(
hass=hass,
device_id=_climate_device,
who=_configured_climate_devices[_climate_device][CONF_WHO],
zone=_configured_climate_devices[_climate_device][CONF_ZONE],
where=_configured_climate_devices[_climate_device][CONF_ZONE],
name=_configured_climate_devices[_climate_device][CONF_NAME],
heating=_configured_climate_devices[_climate_device][CONF_HEATING_SUPPORT],
cooling=_configured_climate_devices[_climate_device][CONF_COOLING_SUPPORT],
Expand Down Expand Up @@ -194,14 +195,14 @@ async def async_unload_entry(hass, config_entry): # pylint: disable=unused-argu
del hass.data[DOMAIN][CONF_ENTITIES][_climate_device]


class MyHOMEClimate(ClimateEntity):
class MyHOMEClimate(MyHOMEEntity, ClimateEntity):
def __init__(
self,
hass,
name: str,
device_id: str,
who: str,
zone: str,
where: str,
heating: bool,
cooling: bool,
fan: bool,
Expand All @@ -211,30 +212,19 @@ def __init__(
model: str,
gateway: MyHOMEGatewayHandler,
):
super().__init__(
hass=hass,
name=name,
device_id=device_id,
who=who,
where=where,
manufacturer=manufacturer,
model=model,
gateway=gateway,
)

self._hass = hass
self._device_id = device_id
self._manufacturer = manufacturer or "BTicino S.p.A."
self._model = model
self._who = who
self._zone = zone
self._standalone = standalone
self._central = True if self._zone == "#0" else central

self._attr_unique_id = self._device_id
self._attr_name = name
self._gateway_handler = gateway

self._attr_device_info = {
"identifiers": {(DOMAIN, self._device_id)},
"name": self._attr_name,
"manufacturer": self._manufacturer,
"model": self._model,
"via_device": (DOMAIN, self._gateway_handler.unique_id),
}

self._attr_entity_registry_enabled_default = True
self._attr_should_poll = False
self._central = True if self._where == "#0" else central

self._attr_temperature_unit = TEMP_CELSIUS
self._attr_precision = 0.1
Expand Down Expand Up @@ -272,22 +262,13 @@ def __init__(

self._attr_fan_mode = None

async def async_added_to_hass(self):
"""When entity is added to hass."""
self._hass.data[DOMAIN][CONF_ENTITIES][self._attr_unique_id] = self
await self.async_update()

async def async_will_remove_from_hass(self):
"""When entity is removed from hass."""
del self._hass.data[DOMAIN][CONF_ENTITIES][self._attr_unique_id]

async def async_update(self):
"""Update the entity.
Only used by the generic entity update service.
"""
await self._gateway_handler.send_status_request(
OWNHeatingCommand.status(self._zone)
OWNHeatingCommand.status(self._where)
)

@property
Expand All @@ -302,13 +283,15 @@ async def async_set_hvac_mode(self, hvac_mode):
if hvac_mode == HVAC_MODE_OFF:
await self._gateway_handler.send(
OWNHeatingCommand.set_mode(
where=self._zone, mode=CLIMATE_MODE_OFF, standalone=self._standalone
where=self._where,
mode=CLIMATE_MODE_OFF,
standalone=self._standalone,
)
)
elif hvac_mode == HVAC_MODE_AUTO:
await self._gateway_handler.send(
OWNHeatingCommand.set_mode(
where=self._zone,
where=self._where,
mode=CLIMATE_MODE_AUTO,
standalone=self._standalone,
)
Expand All @@ -317,7 +300,7 @@ async def async_set_hvac_mode(self, hvac_mode):
if self._target_temperature is not None:
await self._gateway_handler.send(
OWNHeatingCommand.set_temperature(
where=self._zone,
where=self._where,
temperature=self._target_temperature,
mode=CLIMATE_MODE_HEAT,
standalone=self._standalone,
Expand All @@ -327,7 +310,7 @@ async def async_set_hvac_mode(self, hvac_mode):
if self._target_temperature is not None:
await self._gateway_handler.send(
OWNHeatingCommand.set_temperature(
where=self._zone,
where=self._where,
temperature=self._target_temperature,
mode=CLIMATE_MODE_COOL,
standalone=self._standalone,
Expand All @@ -347,7 +330,7 @@ async def async_set_temperature(self, **kwargs):
if self._attr_hvac_mode == HVAC_MODE_HEAT:
await self._gateway_handler.send(
OWNHeatingCommand.set_temperature(
where=self._zone,
where=self._where,
temperature=target_temperature,
mode=CLIMATE_MODE_HEAT,
standalone=self._standalone,
Expand All @@ -356,7 +339,7 @@ async def async_set_temperature(self, **kwargs):
elif self._attr_hvac_mode == HVAC_MODE_COOL:
await self._gateway_handler.send(
OWNHeatingCommand.set_temperature(
where=self._zone,
where=self._where,
temperature=target_temperature,
mode=CLIMATE_MODE_COOL,
standalone=self._standalone,
Expand All @@ -365,7 +348,7 @@ async def async_set_temperature(self, **kwargs):
else:
await self._gateway_handler.send(
OWNHeatingCommand.set_temperature(
where=self._zone,
where=self._where,
temperature=target_temperature,
mode=CLIMATE_MODE_AUTO,
standalone=self._standalone,
Expand Down
Loading

0 comments on commit 271cec4

Please sign in to comment.