Home Assistant Unofficial Reference 2024.12.1
valve.py
Go to the documentation of this file.
1 """Valve for Shelly."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import Any, cast
7 
8 from aioshelly.block_device import Block
9 from aioshelly.const import BLOCK_GENERATIONS, MODEL_GAS
10 
12  ValveDeviceClass,
13  ValveEntity,
14  ValveEntityDescription,
15  ValveEntityFeature,
16 )
17 from homeassistant.core import HomeAssistant, callback
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry
21 from .entity import (
22  BlockEntityDescription,
23  ShellyBlockAttributeEntity,
24  async_setup_block_attribute_entities,
25 )
26 from .utils import async_remove_shelly_entity, get_device_entry_gen
27 
28 
29 @dataclass(kw_only=True, frozen=True)
31  """Class to describe a BLOCK valve."""
32 
33 
34 GAS_VALVE = BlockValveDescription(
35  key="valve|valve",
36  name="Valve",
37  available=lambda block: block.valve not in ("failure", "checking"),
38  removal_condition=lambda _, block: block.valve in ("not_connected", "unknown"),
39 )
40 
41 
43  hass: HomeAssistant,
44  config_entry: ShellyConfigEntry,
45  async_add_entities: AddEntitiesCallback,
46 ) -> None:
47  """Set up valves for device."""
48  if get_device_entry_gen(config_entry) in BLOCK_GENERATIONS:
49  async_setup_block_entry(hass, config_entry, async_add_entities)
50 
51 
52 @callback
54  hass: HomeAssistant,
55  config_entry: ShellyConfigEntry,
56  async_add_entities: AddEntitiesCallback,
57 ) -> None:
58  """Set up valve for device."""
59  coordinator = config_entry.runtime_data.block
60  assert coordinator and coordinator.device.blocks
61 
62  if coordinator.model == MODEL_GAS:
64  hass,
65  async_add_entities,
66  coordinator,
67  {("valve", "valve"): GAS_VALVE},
68  BlockShellyValve,
69  )
70  # Remove deprecated switch entity for gas valve
71  unique_id = f"{coordinator.mac}-valve_0-valve"
72  async_remove_shelly_entity(hass, "switch", unique_id)
73 
74 
76  """Entity that controls a valve on block based Shelly devices."""
77 
78  entity_description: BlockValveDescription
79  _attr_device_class = ValveDeviceClass.GAS
80  _attr_supported_features = ValveEntityFeature.OPEN | ValveEntityFeature.CLOSE
81 
82  def __init__(
83  self,
84  coordinator: ShellyBlockCoordinator,
85  block: Block,
86  attribute: str,
87  description: BlockValveDescription,
88  ) -> None:
89  """Initialize block valve."""
90  super().__init__(coordinator, block, attribute, description)
91  self.control_resultcontrol_result: dict[str, Any] | None = None
92  self._attr_is_closed_attr_is_closed = bool(self.attribute_valueattribute_valueattribute_value == "closed")
93 
94  @property
95  def is_closing(self) -> bool:
96  """Return if the valve is closing."""
97  if self.control_resultcontrol_result:
98  return cast(bool, self.control_resultcontrol_result["state"] == "closing")
99 
100  return self.attribute_valueattribute_valueattribute_value == "closing"
101 
102  @property
103  def is_opening(self) -> bool:
104  """Return if the valve is opening."""
105  if self.control_resultcontrol_result:
106  return cast(bool, self.control_resultcontrol_result["state"] == "opening")
107 
108  return self.attribute_valueattribute_valueattribute_value == "opening"
109 
110  async def async_open_valve(self, **kwargs: Any) -> None:
111  """Open valve."""
112  self.control_resultcontrol_result = await self.set_stateset_state(go="open")
113  self.async_write_ha_stateasync_write_ha_state()
114 
115  async def async_close_valve(self, **kwargs: Any) -> None:
116  """Close valve."""
117  self.control_resultcontrol_result = await self.set_stateset_state(go="close")
118  self.async_write_ha_stateasync_write_ha_state()
119 
120  @callback
121  def _update_callback(self) -> None:
122  """When device updates, clear control result that overrides state."""
123  self.control_resultcontrol_result = None
124  self._attr_is_closed_attr_is_closed = bool(self.attribute_valueattribute_valueattribute_value == "closed")
125  super()._update_callback()
None __init__(self, ShellyBlockCoordinator coordinator, Block block, str attribute, BlockValveDescription description)
Definition: valve.py:88
None async_setup_block_attribute_entities(HomeAssistant hass, AddEntitiesCallback async_add_entities, ShellyBlockCoordinator coordinator, Mapping[tuple[str, str], BlockEntityDescription] sensors, Callable sensor_class)
Definition: entity.py:65
int get_device_entry_gen(ConfigEntry entry)
Definition: utils.py:353
None async_remove_shelly_entity(HomeAssistant hass, str domain, str unique_id)
Definition: utils.py:67
None async_setup_entry(HomeAssistant hass, ShellyConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: valve.py:46
None async_setup_block_entry(HomeAssistant hass, ShellyConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: valve.py:57