hass-core/homeassistant/components/matter/switch.py
hidaris 3bd8955e0e
Add Matter Climate support (#95434)
* Add Matter Climate support

* update set target temp and update callback

* remove print

* remove optional property

* Adjust the code to improve readability.

* add thermostat test

* Remove irrelevant cases in setting the target temperature.

* add temp range support

* update hvac action

* support adjust low high setpoint..

* support set hvac mode

* address some review feedback

* move some methods around

* dont discover climate in switch platform

* set some default values

* fix some of the tests

* fix some typos

* Update thermostat.json

* Update homeassistant/components/matter/climate.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/matter/climate.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* support heat_cool in hvac_modes

* address some review feedback

* handle hvac mode param in set temp service

* check hvac modes by featuremap

* add comment to thermostat feature class

* make ruff happy..

* use enum to enhance readability.

* use builtin feature bitmap

* fix target temp range and address some feedback

* use instance attribute instead of class attr

* make ruff happy...

* address feedback about single case

* add init docstring

* more test

* fix typo in tests

* make ruff happy

* fix hvac modes test

* test case for update callback

* remove optional check

* more tests

* more tests

* update all attributes in the update callback

* Update climate.py

* fix missing test

---------

Co-authored-by: Marcel van der Veldt <m.vanderveldt@outlook.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2023-07-03 12:33:50 +02:00

83 lines
2.7 KiB
Python

"""Matter switches."""
from __future__ import annotations
from typing import Any
from chip.clusters import Objects as clusters
from matter_server.client.models import device_types
from homeassistant.components.switch import (
SwitchDeviceClass,
SwitchEntity,
SwitchEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .entity import MatterEntity
from .helpers import get_matter
from .models import MatterDiscoverySchema
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Matter switches from Config Entry."""
matter = get_matter(hass)
matter.register_platform_handler(Platform.SWITCH, async_add_entities)
class MatterSwitch(MatterEntity, SwitchEntity):
"""Representation of a Matter switch."""
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn switch on."""
await self.matter_client.send_device_command(
node_id=self._endpoint.node.node_id,
endpoint_id=self._endpoint.endpoint_id,
command=clusters.OnOff.Commands.On(),
)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn switch off."""
await self.matter_client.send_device_command(
node_id=self._endpoint.node.node_id,
endpoint_id=self._endpoint.endpoint_id,
command=clusters.OnOff.Commands.Off(),
)
@callback
def _update_from_device(self) -> None:
"""Update from device."""
self._attr_is_on = self.get_matter_attribute_value(
self._entity_info.primary_attribute
)
# Discovery schema(s) to map Matter Attributes to HA entities
DISCOVERY_SCHEMAS = [
MatterDiscoverySchema(
platform=Platform.SWITCH,
entity_description=SwitchEntityDescription(
key="MatterPlug", device_class=SwitchDeviceClass.OUTLET, name=None
),
entity_class=MatterSwitch,
required_attributes=(clusters.OnOff.Attributes.OnOff,),
# restrict device type to prevent discovery by the wrong platform
not_device_type=(
device_types.ColorTemperatureLight,
device_types.DimmableLight,
device_types.ExtendedColorLight,
device_types.OnOffLight,
device_types.DoorLock,
device_types.ColorDimmerSwitch,
device_types.DimmerSwitch,
device_types.OnOffLightSwitch,
device_types.Thermostat,
),
),
]