mirror of
https://github.com/home-assistant/core.git
synced 2026-01-13 10:38:20 +00:00
Compare commits
42 Commits
claude/ext
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
855da1d070 | ||
|
|
d5be76d7e6 | ||
|
|
5f396332df | ||
|
|
56e638e170 | ||
|
|
52b90c7706 | ||
|
|
a6221d16b6 | ||
|
|
51701cab7c | ||
|
|
010e1f2d0d | ||
|
|
66909fc9ca | ||
|
|
90a28c95c8 | ||
|
|
83f2c53e8c | ||
|
|
514b6e243c | ||
|
|
742230c7be | ||
|
|
acb6b1444e | ||
|
|
f358b2231a | ||
|
|
fd24cffa6b | ||
|
|
0b5d6ee538 | ||
|
|
d125bb88d1 | ||
|
|
2ab51f582a | ||
|
|
f9b32811b2 | ||
|
|
41a423e140 | ||
|
|
f717867657 | ||
|
|
ab202a03db | ||
|
|
46a3e5e5b5 | ||
|
|
0163a4d289 | ||
|
|
6c1bf31a3c | ||
|
|
a434760a80 | ||
|
|
798990fadc | ||
|
|
b3d9d92e4a | ||
|
|
1082a9ca69 | ||
|
|
c247f56658 | ||
|
|
e7f71781f1 | ||
|
|
c4b2c5e621 | ||
|
|
7779609a76 | ||
|
|
7b9a5f897c | ||
|
|
6eccbfc1cf | ||
|
|
0da518e951 | ||
|
|
e5851b7920 | ||
|
|
1b9364e8b5 | ||
|
|
8460d4f5e2 | ||
|
|
8fd35cd70d | ||
|
|
88be115699 |
@@ -1,168 +0,0 @@
|
||||
# Claude Code Skills and Reference Files
|
||||
|
||||
This directory contains Claude Skills and reference documentation for working with Home Assistant integrations.
|
||||
|
||||
## Directory Structure
|
||||
|
||||
```
|
||||
.claude/
|
||||
├── skills/ # Claude Skills (auto-loaded)
|
||||
│ ├── testing/
|
||||
│ │ └── SKILL.md # Testing specialist skill
|
||||
│ ├── code-review/
|
||||
│ │ └── SKILL.md # Code review specialist skill
|
||||
│ └── quality-scale-architect/
|
||||
│ └── SKILL.md # Architecture guidance skill
|
||||
├── agents/ # Legacy agent definitions
|
||||
│ └── quality-scale-rule-verifier.md
|
||||
└── references/ # Deep-dive reference docs
|
||||
├── diagnostics.md # Diagnostics implementation
|
||||
├── sensor.md # Sensor platform
|
||||
├── binary_sensor.md # Binary sensor platform
|
||||
├── switch.md # Switch platform
|
||||
├── button.md # Button platform
|
||||
├── number.md # Number platform
|
||||
└── select.md # Select platform
|
||||
```
|
||||
|
||||
## Claude Skills
|
||||
|
||||
Claude Skills are modular capabilities that extend Claude's functionality. Each Skill packages instructions and metadata that Claude uses automatically when relevant.
|
||||
|
||||
### How Skills Work
|
||||
|
||||
Skills use **progressive disclosure** - they load content in stages:
|
||||
|
||||
1. **Level 1 - Metadata (always loaded)**: Skill name and description
|
||||
2. **Level 2 - Instructions (loaded when triggered)**: Main SKILL.md content
|
||||
3. **Level 3+ - Resources (loaded as needed)**: Reference files and additional docs
|
||||
|
||||
This means you can have many Skills installed with minimal context penalty. Claude only knows each Skill exists and when to use it until triggered.
|
||||
|
||||
### Available Skills
|
||||
|
||||
#### Testing (`testing`)
|
||||
**Use when**: Writing, running, or fixing tests for Home Assistant integrations
|
||||
|
||||
Specializes in:
|
||||
- Writing comprehensive test coverage (>95%)
|
||||
- Running pytest with appropriate flags
|
||||
- Fixing failing tests and updating snapshots
|
||||
- Following Home Assistant testing patterns
|
||||
- Modern fixture patterns and snapshot testing
|
||||
|
||||
**Triggers on**: Requests about writing tests, running tests, fixing test failures, test coverage, pytest, snapshots
|
||||
|
||||
#### Code Review (`code-review`)
|
||||
**Use when**: Reviewing code for quality, best practices, and standards compliance
|
||||
|
||||
Specializes in:
|
||||
- Reviewing pull requests and code changes
|
||||
- Identifying anti-patterns and security vulnerabilities
|
||||
- Verifying async patterns and error handling
|
||||
- Ensuring quality scale compliance
|
||||
- Performance optimization
|
||||
|
||||
**Triggers on**: Requests to review code, check for issues, analyze code quality, security review
|
||||
|
||||
#### Quality Scale Architect (`quality-scale-architect`)
|
||||
**Use when**: Needing architectural guidance and quality scale planning
|
||||
|
||||
Specializes in:
|
||||
- High-level architecture guidance
|
||||
- Quality scale tier selection (Bronze/Silver/Gold/Platinum)
|
||||
- Integration structure planning
|
||||
- Pattern recommendations (coordinator, push, hub)
|
||||
- Progression strategies between quality tiers
|
||||
|
||||
**Triggers on**: Requests about architecture, integration design, quality tiers, structural planning, choosing patterns
|
||||
|
||||
## Reference Files
|
||||
|
||||
Reference files provide deep-dive documentation for specific implementation areas. Skills can reference these for detailed guidance, and they're loaded on-demand to avoid consuming context.
|
||||
|
||||
### Available References
|
||||
|
||||
- **diagnostics.md**: Complete guide to implementing integration and device diagnostics, data redaction, testing
|
||||
- **sensor.md**: Sensor platform implementation, device classes, state classes, entity descriptions
|
||||
- **binary_sensor.md**: Binary sensor implementation, device classes, push-updated patterns
|
||||
- **switch.md**: Switch control implementation, state updates, configuration switches
|
||||
- **button.md**: Button action implementation, device classes, one-time actions
|
||||
- **number.md**: Numeric value control, ranges, display modes, units
|
||||
- **select.md**: Option selection implementation, enums, translations, dynamic options
|
||||
|
||||
## How to Use
|
||||
|
||||
### As a Developer
|
||||
|
||||
Skills work automatically - just ask Claude to help with tasks:
|
||||
|
||||
- **Testing**: "Write tests for my sensor platform" or "Fix the failing config flow tests"
|
||||
- **Review**: "Review this integration for security issues" or "Check my async patterns"
|
||||
- **Architecture**: "Help me design a hub integration" or "What quality tier should I target?"
|
||||
|
||||
### As Claude
|
||||
|
||||
Skills are triggered automatically when requests match the skill descriptions. Skills can reference the documentation files in `.claude/references/` for detailed implementation guidance.
|
||||
|
||||
Example:
|
||||
```python
|
||||
# When a testing request comes in, Claude triggers the testing skill
|
||||
# The skill can then reference .claude/references/sensor.md for sensor-specific patterns
|
||||
```
|
||||
|
||||
## Quality Scale Overview
|
||||
|
||||
Home Assistant uses a Quality Scale system:
|
||||
|
||||
- **Bronze**: Basic requirements (mandatory baseline) - Config flow, unique IDs, auth flows
|
||||
- **Silver**: Enhanced functionality - Unavailability tracking, runtime data, parallel updates
|
||||
- **Gold**: Advanced features - Diagnostics, translations, device registry
|
||||
- **Platinum**: Highest quality - Strict typing, async-only dependencies, WebSession injection
|
||||
|
||||
All Bronze rules are mandatory. Higher tiers are additive.
|
||||
|
||||
## Skill Structure
|
||||
|
||||
Each Skill is a directory containing a `SKILL.md` file with YAML frontmatter:
|
||||
|
||||
```yaml
|
||||
---
|
||||
name: skill-name
|
||||
description: Brief description of what this Skill does and when to use it (max 1024 chars)
|
||||
---
|
||||
|
||||
# Skill Content in Markdown
|
||||
|
||||
Instructions, examples, and guidance...
|
||||
```
|
||||
|
||||
**Progressive Loading**: Only the name/description are loaded initially. The full content loads when the Skill is triggered.
|
||||
|
||||
## Creating Custom Skills
|
||||
|
||||
To add a new Skill:
|
||||
|
||||
1. Create a directory: `.claude/skills/my-skill/`
|
||||
2. Add a `SKILL.md` file with proper frontmatter
|
||||
3. Include clear instructions and examples
|
||||
4. Reference existing documentation when appropriate
|
||||
|
||||
See [Claude Skills Documentation](https://platform.claude.com/docs/en/agents-and-tools/agent-skills/overview) for complete guidance.
|
||||
|
||||
## Additional Resources
|
||||
|
||||
- Main instructions: `/home/user/core/CLAUDE.md`
|
||||
- Home Assistant Docs: https://developers.home-assistant.io
|
||||
- Integration Quality Scale: https://developers.home-assistant.io/docs/core/integration-quality-scale/
|
||||
- Claude Skills Cookbook: https://platform.claude.com/cookbook/skills-notebooks-01-skills-introduction
|
||||
|
||||
## Contributing
|
||||
|
||||
When adding new Skills or references:
|
||||
1. Follow the proper Skill structure (SKILL.md with frontmatter)
|
||||
2. Keep descriptions concise and trigger-focused (max 1024 chars)
|
||||
3. Include practical examples in Skill content
|
||||
4. Link to reference documentation for deep dives
|
||||
5. Consider quality scale implications
|
||||
6. Test that Skills trigger appropriately
|
||||
@@ -1,470 +0,0 @@
|
||||
# Binary Sensor Platform Reference
|
||||
|
||||
## Overview
|
||||
|
||||
Binary sensors are read-only entities that represent an on/off, true/false, or open/closed state. They are simpler than regular sensors and don't have units or numeric values.
|
||||
|
||||
## Basic Binary Sensor Implementation
|
||||
|
||||
```python
|
||||
"""Binary sensor platform for my_integration."""
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import MyConfigEntry
|
||||
from .coordinator import MyCoordinator
|
||||
from .entity import MyEntity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: MyConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up binary sensors."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
MyBinarySensor(coordinator, device_id)
|
||||
for device_id in coordinator.data.devices
|
||||
)
|
||||
|
||||
|
||||
class MyBinarySensor(MyEntity, BinarySensorEntity):
|
||||
"""Representation of a binary sensor."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
_attr_translation_key = "motion"
|
||||
_attr_device_class = BinarySensorDeviceClass.MOTION
|
||||
|
||||
def __init__(self, coordinator: MyCoordinator, device_id: str) -> None:
|
||||
"""Initialize the binary sensor."""
|
||||
super().__init__(coordinator, device_id)
|
||||
self._attr_unique_id = f"{device_id}_motion"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if motion is detected."""
|
||||
if device := self.coordinator.data.devices.get(self.device_id):
|
||||
return device.motion_detected
|
||||
return None
|
||||
```
|
||||
|
||||
## Binary Sensor State
|
||||
|
||||
The core property for binary sensors is `is_on`:
|
||||
|
||||
```python
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if the binary sensor is on."""
|
||||
return self.device.is_active
|
||||
|
||||
# Alternatively, use attribute
|
||||
_attr_is_on = True # or False, or None
|
||||
```
|
||||
|
||||
**State Meaning**:
|
||||
- `True` / `"on"` - Active/detected/open
|
||||
- `False` / `"off"` - Inactive/not detected/closed
|
||||
- `None` - Unknown (displays as "unavailable")
|
||||
|
||||
## Device Classes
|
||||
|
||||
Binary sensors should use device classes for proper representation:
|
||||
|
||||
```python
|
||||
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
|
||||
|
||||
# Common device classes
|
||||
_attr_device_class = BinarySensorDeviceClass.MOTION
|
||||
_attr_device_class = BinarySensorDeviceClass.OCCUPANCY
|
||||
_attr_device_class = BinarySensorDeviceClass.DOOR
|
||||
_attr_device_class = BinarySensorDeviceClass.WINDOW
|
||||
_attr_device_class = BinarySensorDeviceClass.OPENING
|
||||
_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
|
||||
_attr_device_class = BinarySensorDeviceClass.BATTERY
|
||||
_attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING
|
||||
_attr_device_class = BinarySensorDeviceClass.PROBLEM
|
||||
_attr_device_class = BinarySensorDeviceClass.RUNNING
|
||||
_attr_device_class = BinarySensorDeviceClass.SMOKE
|
||||
_attr_device_class = BinarySensorDeviceClass.MOISTURE
|
||||
_attr_device_class = BinarySensorDeviceClass.LOCK
|
||||
_attr_device_class = BinarySensorDeviceClass.TAMPER
|
||||
_attr_device_class = BinarySensorDeviceClass.PLUG
|
||||
_attr_device_class = BinarySensorDeviceClass.POWER
|
||||
```
|
||||
|
||||
### Device Class Selection Guide
|
||||
|
||||
**Detection Sensors**:
|
||||
- Motion detector → `MOTION`
|
||||
- Presence detector → `OCCUPANCY`
|
||||
- Smoke detector → `SMOKE`
|
||||
- Water leak detector → `MOISTURE`
|
||||
|
||||
**Contact Sensors**:
|
||||
- Door sensor → `DOOR`
|
||||
- Window sensor → `WINDOW`
|
||||
- Generic contact → `OPENING`
|
||||
|
||||
**Status Sensors**:
|
||||
- Network connection → `CONNECTIVITY`
|
||||
- Device running → `RUNNING`
|
||||
- Low battery → `BATTERY`
|
||||
- Charging state → `BATTERY_CHARGING`
|
||||
- Problem/fault → `PROBLEM`
|
||||
- Tamper detection → `TAMPER`
|
||||
|
||||
**Power Sensors**:
|
||||
- Outlet state → `PLUG`
|
||||
- Power state → `POWER`
|
||||
- Lock state → `LOCK`
|
||||
|
||||
## Entity Descriptions Pattern
|
||||
|
||||
For multiple similar binary sensors:
|
||||
|
||||
```python
|
||||
from dataclasses import dataclass
|
||||
from collections.abc import Callable
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class MyBinarySensorDescription(BinarySensorEntityDescription):
|
||||
"""Describes a binary sensor."""
|
||||
is_on_fn: Callable[[MyData], bool | None]
|
||||
|
||||
|
||||
BINARY_SENSORS: tuple[MyBinarySensorDescription, ...] = (
|
||||
MyBinarySensorDescription(
|
||||
key="motion",
|
||||
translation_key="motion",
|
||||
device_class=BinarySensorDeviceClass.MOTION,
|
||||
is_on_fn=lambda data: data.motion_detected,
|
||||
),
|
||||
MyBinarySensorDescription(
|
||||
key="door",
|
||||
translation_key="door",
|
||||
device_class=BinarySensorDeviceClass.DOOR,
|
||||
is_on_fn=lambda data: data.door_open,
|
||||
),
|
||||
MyBinarySensorDescription(
|
||||
key="battery_low",
|
||||
translation_key="battery_low",
|
||||
device_class=BinarySensorDeviceClass.BATTERY,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
is_on_fn=lambda data: data.battery_level < 20,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: MyConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up binary sensors."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
MyBinarySensor(coordinator, device_id, description)
|
||||
for device_id in coordinator.data.devices
|
||||
for description in BINARY_SENSORS
|
||||
)
|
||||
|
||||
|
||||
class MyBinarySensor(MyEntity, BinarySensorEntity):
|
||||
"""Binary sensor using entity description."""
|
||||
|
||||
entity_description: MyBinarySensorDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: MyCoordinator,
|
||||
device_id: str,
|
||||
description: MyBinarySensorDescription,
|
||||
) -> None:
|
||||
"""Initialize the binary sensor."""
|
||||
super().__init__(coordinator, device_id)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{device_id}_{description.key}"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if the binary sensor is on."""
|
||||
if device := self.coordinator.data.devices.get(self.device_id):
|
||||
return self.entity_description.is_on_fn(device)
|
||||
return None
|
||||
```
|
||||
|
||||
## Entity Category
|
||||
|
||||
Mark diagnostic or configuration binary sensors:
|
||||
|
||||
```python
|
||||
from homeassistant.helpers.entity import EntityCategory
|
||||
|
||||
# Diagnostic sensors
|
||||
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
||||
# Examples: connectivity, update available, battery low
|
||||
|
||||
# Config sensors
|
||||
_attr_entity_category = EntityCategory.CONFIG
|
||||
# Examples: configuration status
|
||||
```
|
||||
|
||||
## State Inversion
|
||||
|
||||
For some sensors, you may need to invert the logic:
|
||||
|
||||
```python
|
||||
class MyBinarySensor(BinarySensorEntity):
|
||||
"""Binary sensor with inverted state."""
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if sensor is on."""
|
||||
if self.device.is_closed:
|
||||
return False # Closed = off for door sensor
|
||||
if self.device.is_open:
|
||||
return True # Open = on for door sensor
|
||||
return None
|
||||
```
|
||||
|
||||
## Push-Updated Binary Sensor
|
||||
|
||||
For event-driven sensors:
|
||||
|
||||
```python
|
||||
class MyPushBinarySensor(BinarySensorEntity):
|
||||
"""Push-updated binary sensor."""
|
||||
|
||||
_attr_should_poll = False
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Subscribe to updates when added."""
|
||||
self.async_on_remove(
|
||||
self.device.subscribe_state(self._handle_state_update)
|
||||
)
|
||||
|
||||
@callback
|
||||
def _handle_state_update(self, state: bool) -> None:
|
||||
"""Handle state update from device."""
|
||||
self._attr_is_on = state
|
||||
self.async_write_ha_state()
|
||||
```
|
||||
|
||||
## Testing Binary Sensors
|
||||
|
||||
### Snapshot Testing
|
||||
|
||||
```python
|
||||
"""Test binary sensors."""
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_binary_sensors(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
init_integration,
|
||||
) -> None:
|
||||
"""Test binary sensor entities."""
|
||||
await snapshot_platform(
|
||||
hass,
|
||||
entity_registry,
|
||||
snapshot,
|
||||
mock_config_entry.entry_id,
|
||||
)
|
||||
```
|
||||
|
||||
### State Testing
|
||||
|
||||
```python
|
||||
async def test_binary_sensor_states(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test binary sensor states."""
|
||||
await init_integration(hass, mock_config_entry)
|
||||
|
||||
# Test on state
|
||||
state = hass.states.get("binary_sensor.my_device_motion")
|
||||
assert state
|
||||
assert state.state == "on"
|
||||
assert state.attributes["device_class"] == "motion"
|
||||
|
||||
# Test off state
|
||||
state = hass.states.get("binary_sensor.my_device_door")
|
||||
assert state
|
||||
assert state.state == "off"
|
||||
assert state.attributes["device_class"] == "door"
|
||||
```
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Pattern 1: Coordinator-Based
|
||||
|
||||
```python
|
||||
class MyBinarySensor(CoordinatorEntity[MyCoordinator], BinarySensorEntity):
|
||||
"""Coordinator-based binary sensor."""
|
||||
|
||||
_attr_should_poll = False
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Get state from coordinator data."""
|
||||
if device := self.coordinator.data.devices.get(self.device_id):
|
||||
return device.is_active
|
||||
return None
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available."""
|
||||
return super().available and self.device_id in self.coordinator.data
|
||||
```
|
||||
|
||||
### Pattern 2: Event-Driven
|
||||
|
||||
```python
|
||||
class MyEventBinarySensor(BinarySensorEntity):
|
||||
"""Event-driven binary sensor."""
|
||||
|
||||
_attr_should_poll = False
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Subscribe to events."""
|
||||
self.async_on_remove(
|
||||
async_dispatcher_connect(
|
||||
self.hass,
|
||||
f"{DOMAIN}_event",
|
||||
self._handle_event,
|
||||
)
|
||||
)
|
||||
|
||||
@callback
|
||||
def _handle_event(self, event_type: str, active: bool) -> None:
|
||||
"""Handle incoming event."""
|
||||
if event_type == self.event_type:
|
||||
self._attr_is_on = active
|
||||
self.async_write_ha_state()
|
||||
```
|
||||
|
||||
### Pattern 3: Calculated/Derived
|
||||
|
||||
```python
|
||||
class MyCalculatedBinarySensor(BinarySensorEntity):
|
||||
"""Binary sensor calculated from other sensors."""
|
||||
|
||||
_attr_should_poll = False
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Subscribe to source sensors."""
|
||||
self.async_on_remove(
|
||||
async_track_state_change_event(
|
||||
self.hass,
|
||||
["sensor.temperature", "sensor.humidity"],
|
||||
self._handle_source_update,
|
||||
)
|
||||
)
|
||||
|
||||
@callback
|
||||
def _handle_source_update(self, event: Event) -> None:
|
||||
"""Recalculate when sources change."""
|
||||
temp = self.hass.states.get("sensor.temperature")
|
||||
humidity = self.hass.states.get("sensor.humidity")
|
||||
|
||||
if temp and humidity:
|
||||
# Example: high comfort if temp 20-25 and humidity 30-60
|
||||
temp_ok = 20 <= float(temp.state) <= 25
|
||||
humidity_ok = 30 <= float(humidity.state) <= 60
|
||||
self._attr_is_on = temp_ok and humidity_ok
|
||||
self.async_write_ha_state()
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### ✅ DO
|
||||
|
||||
- Use appropriate device classes
|
||||
- Return `None` for unknown state
|
||||
- Use `is_on` property (not state)
|
||||
- Implement unique IDs
|
||||
- Use entity descriptions for similar sensors
|
||||
- Mark diagnostic sensors with entity_category
|
||||
- Use translation keys for entity names
|
||||
- Handle availability properly
|
||||
|
||||
### ❌ DON'T
|
||||
|
||||
- Return strings like "on"/"off" from is_on
|
||||
- Use regular Sensor for binary states
|
||||
- Hardcode entity names
|
||||
- Create binary sensors without device classes (when available)
|
||||
- Use unavailable/unknown as state values
|
||||
- Block the event loop
|
||||
- Poll unnecessarily (use coordinator or events)
|
||||
|
||||
## Disabled by Default
|
||||
|
||||
For less important binary sensors:
|
||||
|
||||
```python
|
||||
class MyConnectivitySensor(BinarySensorEntity):
|
||||
"""Connectivity sensor - diagnostic."""
|
||||
|
||||
_attr_entity_registry_enabled_default = False
|
||||
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
||||
_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Binary Sensor Not Appearing
|
||||
|
||||
Check:
|
||||
- [ ] Unique ID is set
|
||||
- [ ] Platform is in PLATFORMS list
|
||||
- [ ] Entity is added with async_add_entities
|
||||
- [ ] is_on returns bool or None (not string)
|
||||
|
||||
### State Not Updating
|
||||
|
||||
Check:
|
||||
- [ ] Coordinator is updating (if used)
|
||||
- [ ] Event subscriptions are working
|
||||
- [ ] is_on returns correct value
|
||||
- [ ] async_write_ha_state() is called (push updates)
|
||||
|
||||
### Wrong Icon
|
||||
|
||||
Check:
|
||||
- [ ] Device class is set correctly
|
||||
- [ ] Device class matches sensor purpose
|
||||
- [ ] Icon translations if using Gold tier
|
||||
|
||||
## Quality Scale Considerations
|
||||
|
||||
- **Bronze**: Unique ID required
|
||||
- **Gold**: Entity translations, device class, entity category
|
||||
- **Platinum**: Full type hints
|
||||
|
||||
## References
|
||||
|
||||
- [Binary Sensor Documentation](https://developers.home-assistant.io/docs/core/entity/binary-sensor)
|
||||
- [Device Classes](https://www.home-assistant.io/integrations/binary_sensor/#device-class)
|
||||
@@ -1,459 +0,0 @@
|
||||
# Button Platform Reference
|
||||
|
||||
## Overview
|
||||
|
||||
Buttons are entities that trigger an action when pressed. They don't have a state (on/off) and are used for one-time actions like rebooting a device, triggering an update, or running a routine.
|
||||
|
||||
## Basic Button Implementation
|
||||
|
||||
```python
|
||||
"""Button platform for my_integration."""
|
||||
from homeassistant.components.button import ButtonEntity
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import MyConfigEntry
|
||||
from .coordinator import MyCoordinator
|
||||
from .entity import MyEntity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: MyConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up buttons."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
MyButton(coordinator, device_id)
|
||||
for device_id in coordinator.data.devices
|
||||
)
|
||||
|
||||
|
||||
class MyButton(MyEntity, ButtonEntity):
|
||||
"""Representation of a button."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
_attr_translation_key = "reboot"
|
||||
|
||||
def __init__(self, coordinator: MyCoordinator, device_id: str) -> None:
|
||||
"""Initialize the button."""
|
||||
super().__init__(coordinator, device_id)
|
||||
self._attr_unique_id = f"{device_id}_reboot"
|
||||
|
||||
async def async_press(self) -> None:
|
||||
"""Handle the button press."""
|
||||
await self.coordinator.client.reboot(self.device_id)
|
||||
```
|
||||
|
||||
## Button Method
|
||||
|
||||
The only required method for buttons:
|
||||
|
||||
```python
|
||||
async def async_press(self) -> None:
|
||||
"""Handle the button press."""
|
||||
await self.device.trigger_action()
|
||||
```
|
||||
|
||||
**Note**: Buttons don't have state. They only perform an action when pressed.
|
||||
|
||||
## Device Class
|
||||
|
||||
Buttons can have device classes to indicate their purpose:
|
||||
|
||||
```python
|
||||
from homeassistant.components.button import ButtonDeviceClass
|
||||
|
||||
_attr_device_class = ButtonDeviceClass.RESTART
|
||||
_attr_device_class = ButtonDeviceClass.UPDATE
|
||||
_attr_device_class = ButtonDeviceClass.IDENTIFY
|
||||
```
|
||||
|
||||
Device classes:
|
||||
- `RESTART` - Reboot/restart device
|
||||
- `UPDATE` - Trigger update check or installation
|
||||
- `IDENTIFY` - Make device identify itself (blink LED, beep, etc.)
|
||||
|
||||
## Entity Category
|
||||
|
||||
Most buttons are configuration actions:
|
||||
|
||||
```python
|
||||
from homeassistant.helpers.entity import EntityCategory
|
||||
|
||||
# Config buttons (device settings/actions)
|
||||
_attr_entity_category = EntityCategory.CONFIG
|
||||
# Examples: reboot, reset, identify
|
||||
|
||||
# Diagnostic buttons (troubleshooting)
|
||||
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
||||
# Examples: test connection, refresh diagnostics
|
||||
```
|
||||
|
||||
## Entity Descriptions Pattern
|
||||
|
||||
For multiple buttons:
|
||||
|
||||
```python
|
||||
from dataclasses import dataclass
|
||||
from collections.abc import Awaitable, Callable
|
||||
|
||||
from homeassistant.components.button import ButtonEntityDescription, ButtonDeviceClass
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class MyButtonDescription(ButtonEntityDescription):
|
||||
"""Describes a button."""
|
||||
press_fn: Callable[[MyClient, str], Awaitable[None]]
|
||||
|
||||
|
||||
BUTTONS: tuple[MyButtonDescription, ...] = (
|
||||
MyButtonDescription(
|
||||
key="reboot",
|
||||
translation_key="reboot",
|
||||
device_class=ButtonDeviceClass.RESTART,
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
press_fn=lambda client, device_id: client.reboot(device_id),
|
||||
),
|
||||
MyButtonDescription(
|
||||
key="identify",
|
||||
translation_key="identify",
|
||||
device_class=ButtonDeviceClass.IDENTIFY,
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
press_fn=lambda client, device_id: client.identify(device_id),
|
||||
),
|
||||
MyButtonDescription(
|
||||
key="check_update",
|
||||
translation_key="check_update",
|
||||
device_class=ButtonDeviceClass.UPDATE,
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
press_fn=lambda client, device_id: client.check_updates(device_id),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: MyConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up buttons."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
MyButton(coordinator, device_id, description)
|
||||
for device_id in coordinator.data.devices
|
||||
for description in BUTTONS
|
||||
)
|
||||
|
||||
|
||||
class MyButton(MyEntity, ButtonEntity):
|
||||
"""Button using entity description."""
|
||||
|
||||
entity_description: MyButtonDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: MyCoordinator,
|
||||
device_id: str,
|
||||
description: MyButtonDescription,
|
||||
) -> None:
|
||||
"""Initialize the button."""
|
||||
super().__init__(coordinator, device_id)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{device_id}_{description.key}"
|
||||
|
||||
async def async_press(self) -> None:
|
||||
"""Handle the button press."""
|
||||
await self.entity_description.press_fn(
|
||||
self.coordinator.client,
|
||||
self.device_id,
|
||||
)
|
||||
```
|
||||
|
||||
## Common Button Types
|
||||
|
||||
### Restart Button
|
||||
|
||||
```python
|
||||
class RestartButton(ButtonEntity):
|
||||
"""Restart device button."""
|
||||
|
||||
_attr_device_class = ButtonDeviceClass.RESTART
|
||||
_attr_entity_category = EntityCategory.CONFIG
|
||||
_attr_translation_key = "restart"
|
||||
|
||||
async def async_press(self) -> None:
|
||||
"""Restart the device."""
|
||||
await self.device.restart()
|
||||
```
|
||||
|
||||
### Update Button
|
||||
|
||||
```python
|
||||
class UpdateButton(ButtonEntity):
|
||||
"""Trigger update check button."""
|
||||
|
||||
_attr_device_class = ButtonDeviceClass.UPDATE
|
||||
_attr_entity_category = EntityCategory.CONFIG
|
||||
_attr_translation_key = "check_update"
|
||||
|
||||
async def async_press(self) -> None:
|
||||
"""Check for updates."""
|
||||
await self.device.check_for_updates()
|
||||
```
|
||||
|
||||
### Identify Button
|
||||
|
||||
```python
|
||||
class IdentifyButton(ButtonEntity):
|
||||
"""Make device identify itself."""
|
||||
|
||||
_attr_device_class = ButtonDeviceClass.IDENTIFY
|
||||
_attr_entity_category = EntityCategory.CONFIG
|
||||
_attr_translation_key = "identify"
|
||||
|
||||
async def async_press(self) -> None:
|
||||
"""Trigger device identification."""
|
||||
await self.device.identify()
|
||||
```
|
||||
|
||||
### Custom Action Button
|
||||
|
||||
```python
|
||||
class CustomButton(ButtonEntity):
|
||||
"""Custom action button."""
|
||||
|
||||
_attr_entity_category = EntityCategory.CONFIG
|
||||
_attr_translation_key = "run_cycle"
|
||||
|
||||
async def async_press(self) -> None:
|
||||
"""Run cleaning cycle."""
|
||||
await self.device.start_cleaning_cycle()
|
||||
```
|
||||
|
||||
## State Updates After Press
|
||||
|
||||
Buttons trigger coordinator refresh if needed:
|
||||
|
||||
```python
|
||||
async def async_press(self) -> None:
|
||||
"""Handle press with refresh."""
|
||||
await self.coordinator.client.reboot(self.device_id)
|
||||
# Refresh coordinator to update related entities
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
Handle errors appropriately:
|
||||
|
||||
```python
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
|
||||
|
||||
async def async_press(self) -> None:
|
||||
"""Handle press with error handling."""
|
||||
try:
|
||||
await self.device.reboot()
|
||||
except DeviceOfflineError as err:
|
||||
raise HomeAssistantError(f"Device is offline: {err}") from err
|
||||
except DeviceError as err:
|
||||
raise HomeAssistantError(f"Failed to reboot: {err}") from err
|
||||
```
|
||||
|
||||
## Testing Buttons
|
||||
|
||||
### Snapshot Testing
|
||||
|
||||
```python
|
||||
"""Test buttons."""
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_buttons(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
init_integration,
|
||||
) -> None:
|
||||
"""Test button entities."""
|
||||
await snapshot_platform(
|
||||
hass,
|
||||
entity_registry,
|
||||
snapshot,
|
||||
mock_config_entry.entry_id,
|
||||
)
|
||||
```
|
||||
|
||||
### Press Testing
|
||||
|
||||
```python
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
|
||||
|
||||
|
||||
async def test_button_press(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_device,
|
||||
) -> None:
|
||||
"""Test button press."""
|
||||
await init_integration(hass, mock_config_entry)
|
||||
|
||||
# Press button
|
||||
await hass.services.async_call(
|
||||
BUTTON_DOMAIN,
|
||||
SERVICE_PRESS,
|
||||
{ATTR_ENTITY_ID: "button.my_device_reboot"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
# Verify action was called
|
||||
mock_device.reboot.assert_called_once()
|
||||
```
|
||||
|
||||
### Error Testing
|
||||
|
||||
```python
|
||||
async def test_button_press_error(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_device,
|
||||
) -> None:
|
||||
"""Test button press with error."""
|
||||
await init_integration(hass, mock_config_entry)
|
||||
|
||||
mock_device.reboot.side_effect = DeviceError("Connection failed")
|
||||
|
||||
# Press button should raise error
|
||||
with pytest.raises(HomeAssistantError):
|
||||
await hass.services.async_call(
|
||||
BUTTON_DOMAIN,
|
||||
SERVICE_PRESS,
|
||||
{ATTR_ENTITY_ID: "button.my_device_reboot"},
|
||||
blocking=True,
|
||||
)
|
||||
```
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Pattern 1: Simple Action Button
|
||||
|
||||
```python
|
||||
class SimpleButton(ButtonEntity):
|
||||
"""Simple button that triggers action."""
|
||||
|
||||
async def async_press(self) -> None:
|
||||
"""Trigger action."""
|
||||
await self.device.do_something()
|
||||
```
|
||||
|
||||
### Pattern 2: Button with Coordinator Refresh
|
||||
|
||||
```python
|
||||
class RefreshingButton(CoordinatorEntity[MyCoordinator], ButtonEntity):
|
||||
"""Button that refreshes coordinator."""
|
||||
|
||||
async def async_press(self) -> None:
|
||||
"""Trigger action and refresh."""
|
||||
await self.coordinator.client.action(self.device_id)
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
### Pattern 3: Button with Validation
|
||||
|
||||
```python
|
||||
class ValidatingButton(ButtonEntity):
|
||||
"""Button with pre-action validation."""
|
||||
|
||||
async def async_press(self) -> None:
|
||||
"""Validate then trigger action."""
|
||||
if not self.device.is_ready:
|
||||
raise HomeAssistantError("Device not ready")
|
||||
|
||||
await self.device.trigger_action()
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### ✅ DO
|
||||
|
||||
- Use appropriate device class
|
||||
- Set entity category (usually CONFIG)
|
||||
- Handle errors with HomeAssistantError
|
||||
- Implement unique IDs
|
||||
- Use translation keys
|
||||
- Refresh coordinator if state changes
|
||||
- Provide clear button names/translations
|
||||
|
||||
### ❌ DON'T
|
||||
|
||||
- Create buttons that track state (use switch instead)
|
||||
- Poll buttons (they have no state)
|
||||
- Block the event loop
|
||||
- Ignore errors silently
|
||||
- Create buttons without entity category
|
||||
- Hardcode entity names
|
||||
- Use buttons for binary controls (use switch)
|
||||
|
||||
## Button vs. Switch vs. Service
|
||||
|
||||
**Use Button when**:
|
||||
- One-time action with no state
|
||||
- Trigger command (reboot, identify)
|
||||
- User initiates action
|
||||
|
||||
**Use Switch when**:
|
||||
- Binary control (on/off)
|
||||
- State matters
|
||||
- Can be turned on and off
|
||||
|
||||
**Use Service when**:
|
||||
- Complex parameters needed
|
||||
- Multiple related actions
|
||||
- Integration-wide operations
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Button Not Appearing
|
||||
|
||||
Check:
|
||||
- [ ] Unique ID is set
|
||||
- [ ] Platform is in PLATFORMS list
|
||||
- [ ] Entity is added with async_add_entities
|
||||
- [ ] async_press is implemented
|
||||
|
||||
### Button Press Not Working
|
||||
|
||||
Check:
|
||||
- [ ] async_press is async def
|
||||
- [ ] Not blocking the event loop
|
||||
- [ ] API client is working
|
||||
- [ ] Errors are being raised properly
|
||||
|
||||
### Button Not in Expected Category
|
||||
|
||||
Check:
|
||||
- [ ] entity_category is set
|
||||
- [ ] Using correct EntityCategory value
|
||||
- [ ] Device class is appropriate
|
||||
|
||||
## Quality Scale Considerations
|
||||
|
||||
- **Bronze**: Unique ID required
|
||||
- **Gold**: Entity translations, device class, entity category
|
||||
- **Platinum**: Full type hints
|
||||
|
||||
## References
|
||||
|
||||
- [Button Documentation](https://developers.home-assistant.io/docs/core/entity/button)
|
||||
- [Button Integration](https://www.home-assistant.io/integrations/button/)
|
||||
@@ -1,420 +0,0 @@
|
||||
# Diagnostics Reference
|
||||
|
||||
## Overview
|
||||
|
||||
Diagnostics provide a way to collect and export integration data for troubleshooting purposes. This is a **Gold tier** quality scale requirement that helps users and developers debug issues.
|
||||
|
||||
## When to Implement Diagnostics
|
||||
|
||||
Diagnostics are required for:
|
||||
- ✅ Gold tier and above integrations
|
||||
- ✅ Any integration where users might need support
|
||||
- ✅ Integrations with complex configuration or state
|
||||
|
||||
## Diagnostics Types
|
||||
|
||||
Home Assistant supports two types of diagnostics:
|
||||
|
||||
### 1. Config Entry Diagnostics
|
||||
Provides data about a specific configuration entry.
|
||||
|
||||
**File**: `diagnostics.py` in your integration folder
|
||||
|
||||
```python
|
||||
"""Diagnostics support for My Integration."""
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
TO_REDACT = {
|
||||
"api_key",
|
||||
"access_token",
|
||||
"refresh_token",
|
||||
"password",
|
||||
"username",
|
||||
"email",
|
||||
"latitude",
|
||||
"longitude",
|
||||
}
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
return {
|
||||
"entry": {
|
||||
"title": entry.title,
|
||||
"data": async_redact_data(entry.data, TO_REDACT),
|
||||
"options": async_redact_data(entry.options, TO_REDACT),
|
||||
},
|
||||
"coordinator_data": coordinator.data.to_dict(),
|
||||
"last_update_success": coordinator.last_update_success,
|
||||
"last_update": coordinator.last_update_success_time.isoformat()
|
||||
if coordinator.last_update_success_time
|
||||
else None,
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Device Diagnostics
|
||||
Provides data about a specific device.
|
||||
|
||||
```python
|
||||
async def async_get_device_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
device: dr.DeviceEntry,
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a device."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
# Find device identifier
|
||||
device_id = None
|
||||
for identifier in device.identifiers:
|
||||
if identifier[0] == DOMAIN:
|
||||
device_id = identifier[1]
|
||||
break
|
||||
|
||||
if device_id is None:
|
||||
return {}
|
||||
|
||||
device_data = coordinator.data.devices.get(device_id)
|
||||
if device_data is None:
|
||||
return {}
|
||||
|
||||
return {
|
||||
"device_info": {
|
||||
"id": device_id,
|
||||
"name": device_data.name,
|
||||
"model": device_data.model,
|
||||
"firmware": device_data.firmware_version,
|
||||
},
|
||||
"device_data": device_data.to_dict(),
|
||||
"entities": [
|
||||
{
|
||||
"entity_id": entity.entity_id,
|
||||
"name": entity.name,
|
||||
"state": hass.states.get(entity.entity_id).state
|
||||
if (state := hass.states.get(entity.entity_id))
|
||||
else None,
|
||||
}
|
||||
for entity in er.async_entries_for_device(
|
||||
er.async_get(hass), device.id, include_disabled_entities=True
|
||||
)
|
||||
],
|
||||
}
|
||||
```
|
||||
|
||||
## Data Redaction
|
||||
|
||||
**CRITICAL**: Always redact sensitive information!
|
||||
|
||||
### What to Redact
|
||||
|
||||
Always redact:
|
||||
- API keys, tokens, secrets
|
||||
- Passwords, credentials
|
||||
- Email addresses, usernames
|
||||
- Precise GPS coordinates (latitude, longitude)
|
||||
- MAC addresses (sometimes)
|
||||
- Serial numbers (if sensitive)
|
||||
- Personal information
|
||||
|
||||
### Using async_redact_data
|
||||
|
||||
```python
|
||||
from homeassistant.helpers import async_redact_data
|
||||
|
||||
# Basic redaction
|
||||
data = async_redact_data(entry.data, TO_REDACT)
|
||||
|
||||
# With nested redaction
|
||||
TO_REDACT = {
|
||||
"api_key",
|
||||
"auth.password", # Nested key
|
||||
"user.email", # Nested key
|
||||
}
|
||||
|
||||
# Redacting from multiple sources
|
||||
diagnostics = {
|
||||
"config": async_redact_data(entry.data, TO_REDACT),
|
||||
"options": async_redact_data(entry.options, TO_REDACT),
|
||||
"coordinator": async_redact_data(coordinator.data, TO_REDACT),
|
||||
}
|
||||
```
|
||||
|
||||
### Custom Redaction
|
||||
|
||||
For complex data structures:
|
||||
|
||||
```python
|
||||
def redact_device_data(data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Redact sensitive device data."""
|
||||
redacted = data.copy()
|
||||
|
||||
# Redact specific fields
|
||||
if "serial_number" in redacted:
|
||||
redacted["serial_number"] = "**REDACTED**"
|
||||
|
||||
# Redact nested structures
|
||||
if "location" in redacted:
|
||||
redacted["location"] = {
|
||||
"city": redacted["location"].get("city"),
|
||||
# Don't include exact coordinates
|
||||
}
|
||||
|
||||
return redacted
|
||||
```
|
||||
|
||||
## What to Include
|
||||
|
||||
### Good Diagnostic Data
|
||||
|
||||
Include information helpful for troubleshooting:
|
||||
- ✅ Integration version/state
|
||||
- ✅ Configuration (redacted)
|
||||
- ✅ Coordinator/connection status
|
||||
- ✅ Device information (model, firmware)
|
||||
- ✅ API response examples (redacted)
|
||||
- ✅ Error states
|
||||
- ✅ Entity states
|
||||
- ✅ Feature flags/capabilities
|
||||
|
||||
### Example Comprehensive Diagnostics
|
||||
|
||||
```python
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
return {
|
||||
# Integration state
|
||||
"integration": {
|
||||
"version": coordinator.version,
|
||||
"entry_id": entry.entry_id,
|
||||
"title": entry.title,
|
||||
"state": entry.state,
|
||||
},
|
||||
# Configuration (redacted)
|
||||
"configuration": {
|
||||
"data": async_redact_data(entry.data, TO_REDACT),
|
||||
"options": async_redact_data(entry.options, TO_REDACT),
|
||||
},
|
||||
# Connection/Coordinator status
|
||||
"coordinator": {
|
||||
"last_update_success": coordinator.last_update_success,
|
||||
"last_update": coordinator.last_update_success_time.isoformat()
|
||||
if coordinator.last_update_success_time
|
||||
else None,
|
||||
"update_interval": coordinator.update_interval.total_seconds(),
|
||||
"last_exception": str(coordinator.last_exception)
|
||||
if coordinator.last_exception
|
||||
else None,
|
||||
},
|
||||
# Device/System information
|
||||
"devices": {
|
||||
device_id: {
|
||||
"name": device.name,
|
||||
"model": device.model,
|
||||
"firmware": device.firmware,
|
||||
"features": device.supported_features,
|
||||
"state": device.state,
|
||||
}
|
||||
for device_id, device in coordinator.data.devices.items()
|
||||
},
|
||||
# API information (redacted)
|
||||
"api": {
|
||||
"endpoint": coordinator.client.endpoint,
|
||||
"authenticated": coordinator.client.is_authenticated,
|
||||
"rate_limit_remaining": coordinator.client.rate_limit_remaining,
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
## Testing Diagnostics
|
||||
|
||||
### Test File Structure
|
||||
|
||||
```python
|
||||
"""Test diagnostics."""
|
||||
from homeassistant.core import HomeAssistant
|
||||
from pytest_homeassistant_custom_component.common import MockConfigEntry
|
||||
|
||||
from tests.components.my_integration import setup_integration
|
||||
|
||||
|
||||
async def test_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_api,
|
||||
) -> None:
|
||||
"""Test config entry diagnostics."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
diagnostics = await get_diagnostics_for_config_entry(
|
||||
hass, hass_client, mock_config_entry
|
||||
)
|
||||
|
||||
# Verify structure
|
||||
assert "entry" in diagnostics
|
||||
assert "coordinator_data" in diagnostics
|
||||
|
||||
# Verify redaction
|
||||
assert "api_key" not in str(diagnostics)
|
||||
assert "password" not in str(diagnostics)
|
||||
|
||||
# Verify useful data is present
|
||||
assert diagnostics["entry"]["title"] == "My Device"
|
||||
assert diagnostics["coordinator_data"]["devices"]
|
||||
|
||||
|
||||
async def test_device_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
) -> None:
|
||||
"""Test device diagnostics."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
device = device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, "device_id")}
|
||||
)
|
||||
assert device
|
||||
|
||||
diagnostics = await get_diagnostics_for_device(
|
||||
hass, hass_client, mock_config_entry, device
|
||||
)
|
||||
|
||||
# Verify device-specific data
|
||||
assert diagnostics["device_info"]["id"] == "device_id"
|
||||
assert "entities" in diagnostics
|
||||
```
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Pattern 1: Coordinator-Based Integration
|
||||
|
||||
```python
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
return {
|
||||
"coordinator": {
|
||||
"last_update_success": coordinator.last_update_success,
|
||||
"data": coordinator.data.to_dict(),
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Pattern 2: Multiple Coordinators
|
||||
|
||||
```python
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
data = entry.runtime_data
|
||||
|
||||
return {
|
||||
"device_coordinator": data.device_coordinator.data.to_dict(),
|
||||
"status_coordinator": data.status_coordinator.data.to_dict(),
|
||||
}
|
||||
```
|
||||
|
||||
### Pattern 3: Hub with Multiple Devices
|
||||
|
||||
```python
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
hub = entry.runtime_data
|
||||
|
||||
return {
|
||||
"hub": {
|
||||
"connected": hub.connected,
|
||||
"version": hub.version,
|
||||
},
|
||||
"devices": {
|
||||
device_id: device.to_dict()
|
||||
for device_id, device in hub.devices.items()
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### ✅ DO
|
||||
|
||||
- Redact all sensitive information
|
||||
- Include coordinator state and update times
|
||||
- Provide device/system information
|
||||
- Include error messages (if present)
|
||||
- Make data easily readable
|
||||
- Test that redaction works
|
||||
- Include API/connection status
|
||||
|
||||
### ❌ DON'T
|
||||
|
||||
- Include raw passwords, tokens, or API keys
|
||||
- Include precise GPS coordinates
|
||||
- Include personal information (emails, names)
|
||||
- Make diagnostics too large (>1MB)
|
||||
- Include binary data
|
||||
- Assume all fields are present (use .get())
|
||||
- Include sensitive serial numbers
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Diagnostics Not Appearing
|
||||
|
||||
Check:
|
||||
1. File named `diagnostics.py` in integration folder
|
||||
2. Function named exactly `async_get_config_entry_diagnostics`
|
||||
3. Proper import of `ConfigEntry` and `HomeAssistant`
|
||||
4. Integration is loaded successfully
|
||||
|
||||
### Redaction Not Working
|
||||
|
||||
Check:
|
||||
1. Using `async_redact_data` from `homeassistant.helpers`
|
||||
2. Field names match exactly (case-sensitive)
|
||||
3. Nested fields use dot notation: `"auth.password"`
|
||||
4. TO_REDACT is a set, not a list
|
||||
|
||||
### Device Diagnostics Not Working
|
||||
|
||||
Check:
|
||||
1. Device has proper identifiers
|
||||
2. Function named exactly `async_get_device_diagnostics`
|
||||
3. Device parameter is `dr.DeviceEntry`
|
||||
4. Proper device lookup logic
|
||||
|
||||
## Quality Scale Considerations
|
||||
|
||||
Diagnostics are required for **Gold tier** integrations:
|
||||
- Must implement config entry diagnostics
|
||||
- Should implement device diagnostics (if applicable)
|
||||
- Must redact all sensitive information
|
||||
- Should provide comprehensive troubleshooting data
|
||||
|
||||
## References
|
||||
|
||||
- Quality Scale Rule: `diagnostics`
|
||||
- Home Assistant Docs: [Integration Diagnostics](https://developers.home-assistant.io/docs/integration_fetching_data)
|
||||
- Helper Functions: `homeassistant.helpers.redact`
|
||||
@@ -1,508 +0,0 @@
|
||||
# Number Platform Reference
|
||||
|
||||
## Overview
|
||||
|
||||
Number entities allow users to control numeric values within a defined range. They're used for settings like volume, brightness, temperature setpoints, or any numeric configuration parameter.
|
||||
|
||||
## Basic Number Implementation
|
||||
|
||||
```python
|
||||
"""Number platform for my_integration."""
|
||||
from homeassistant.components.number import NumberEntity, NumberMode
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import MyConfigEntry
|
||||
from .coordinator import MyCoordinator
|
||||
from .entity import MyEntity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: MyConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up numbers."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
MyNumber(coordinator, device_id)
|
||||
for device_id in coordinator.data.devices
|
||||
)
|
||||
|
||||
|
||||
class MyNumber(MyEntity, NumberEntity):
|
||||
"""Representation of a number."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
_attr_translation_key = "volume"
|
||||
_attr_native_min_value = 0
|
||||
_attr_native_max_value = 100
|
||||
_attr_native_step = 1
|
||||
|
||||
def __init__(self, coordinator: MyCoordinator, device_id: str) -> None:
|
||||
"""Initialize the number."""
|
||||
super().__init__(coordinator, device_id)
|
||||
self._attr_unique_id = f"{device_id}_volume"
|
||||
|
||||
@property
|
||||
def native_value(self) -> float | None:
|
||||
"""Return the current value."""
|
||||
if device := self.coordinator.data.devices.get(self.device_id):
|
||||
return device.volume
|
||||
return None
|
||||
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Set new value."""
|
||||
await self.coordinator.client.set_volume(self.device_id, int(value))
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
## Number Properties
|
||||
|
||||
### Core Properties
|
||||
|
||||
```python
|
||||
class MyNumber(NumberEntity):
|
||||
"""Number with all common properties."""
|
||||
|
||||
# Basic identification
|
||||
_attr_has_entity_name = True
|
||||
_attr_translation_key = "brightness"
|
||||
_attr_unique_id = "device_123_brightness"
|
||||
|
||||
# Value range and step
|
||||
_attr_native_min_value = 0
|
||||
_attr_native_max_value = 255
|
||||
_attr_native_step = 1 # or 0.1 for decimals
|
||||
|
||||
# Unit of measurement
|
||||
_attr_native_unit_of_measurement = PERCENTAGE # or other units
|
||||
|
||||
# Display mode
|
||||
_attr_mode = NumberMode.SLIDER # or NumberMode.BOX, NumberMode.AUTO
|
||||
|
||||
# Entity category
|
||||
_attr_entity_category = EntityCategory.CONFIG
|
||||
|
||||
@property
|
||||
def native_value(self) -> float | None:
|
||||
"""Return current value."""
|
||||
return self.device.brightness
|
||||
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Set new value."""
|
||||
await self.device.set_brightness(int(value))
|
||||
```
|
||||
|
||||
### Required Properties
|
||||
|
||||
```python
|
||||
# Minimum value
|
||||
_attr_native_min_value = 0
|
||||
|
||||
# Maximum value
|
||||
_attr_native_max_value = 100
|
||||
|
||||
# Step size (precision)
|
||||
_attr_native_step = 1 # Integers
|
||||
_attr_native_step = 0.1 # One decimal place
|
||||
_attr_native_step = 0.01 # Two decimal places
|
||||
```
|
||||
|
||||
### Current Value
|
||||
|
||||
```python
|
||||
@property
|
||||
def native_value(self) -> float | None:
|
||||
"""Return the current value."""
|
||||
return self.device.current_value
|
||||
|
||||
# Or use attribute
|
||||
_attr_native_value = 50.0
|
||||
```
|
||||
|
||||
### Set Value Method
|
||||
|
||||
```python
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Update to new value."""
|
||||
await self.device.set_value(value)
|
||||
# Update state
|
||||
self._attr_native_value = value
|
||||
self.async_write_ha_state()
|
||||
```
|
||||
|
||||
## Display Mode
|
||||
|
||||
Control how the number is displayed in the UI:
|
||||
|
||||
```python
|
||||
from homeassistant.components.number import NumberMode
|
||||
|
||||
# Slider (default for ranges)
|
||||
_attr_mode = NumberMode.SLIDER
|
||||
|
||||
# Input box (better for precise values or large ranges)
|
||||
_attr_mode = NumberMode.BOX
|
||||
|
||||
# Auto (let HA decide based on range)
|
||||
_attr_mode = NumberMode.AUTO
|
||||
```
|
||||
|
||||
**When to use each**:
|
||||
- `SLIDER`: Small ranges (0-100), settings like volume/brightness
|
||||
- `BOX`: Large ranges, precise values, IDs or codes
|
||||
- `AUTO`: Let Home Assistant decide (default)
|
||||
|
||||
## Device Class
|
||||
|
||||
Use device classes for proper representation:
|
||||
|
||||
```python
|
||||
from homeassistant.components.number import NumberDeviceClass
|
||||
|
||||
# Common device classes
|
||||
_attr_device_class = NumberDeviceClass.TEMPERATURE
|
||||
_attr_device_class = NumberDeviceClass.HUMIDITY
|
||||
_attr_device_class = NumberDeviceClass.VOLTAGE
|
||||
_attr_device_class = NumberDeviceClass.CURRENT
|
||||
_attr_device_class = NumberDeviceClass.POWER
|
||||
_attr_device_class = NumberDeviceClass.BATTERY
|
||||
_attr_device_class = NumberDeviceClass.DISTANCE
|
||||
_attr_device_class = NumberDeviceClass.DURATION
|
||||
```
|
||||
|
||||
## Units of Measurement
|
||||
|
||||
```python
|
||||
from homeassistant.const import (
|
||||
PERCENTAGE,
|
||||
UnitOfTemperature,
|
||||
UnitOfTime,
|
||||
)
|
||||
|
||||
# Percentage (0-100)
|
||||
_attr_native_unit_of_measurement = PERCENTAGE
|
||||
|
||||
# Temperature
|
||||
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
|
||||
|
||||
# Time
|
||||
_attr_native_unit_of_measurement = UnitOfTime.SECONDS
|
||||
|
||||
# Custom units
|
||||
_attr_native_unit_of_measurement = "dB" # Decibels
|
||||
```
|
||||
|
||||
## Entity Descriptions Pattern
|
||||
|
||||
For multiple number entities:
|
||||
|
||||
```python
|
||||
from dataclasses import dataclass
|
||||
from collections.abc import Awaitable, Callable
|
||||
|
||||
from homeassistant.components.number import NumberEntityDescription, NumberMode
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class MyNumberDescription(NumberEntityDescription):
|
||||
"""Describes a number."""
|
||||
value_fn: Callable[[MyData], float | None]
|
||||
set_fn: Callable[[MyClient, str, float], Awaitable[None]]
|
||||
|
||||
|
||||
NUMBERS: tuple[MyNumberDescription, ...] = (
|
||||
MyNumberDescription(
|
||||
key="volume",
|
||||
translation_key="volume",
|
||||
native_min_value=0,
|
||||
native_max_value=100,
|
||||
native_step=1,
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
mode=NumberMode.SLIDER,
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
value_fn=lambda data: data.volume,
|
||||
set_fn=lambda client, device_id, value: client.set_volume(device_id, int(value)),
|
||||
),
|
||||
MyNumberDescription(
|
||||
key="temperature_setpoint",
|
||||
translation_key="temperature_setpoint",
|
||||
device_class=NumberDeviceClass.TEMPERATURE,
|
||||
native_min_value=16,
|
||||
native_max_value=30,
|
||||
native_step=0.5,
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
mode=NumberMode.SLIDER,
|
||||
value_fn=lambda data: data.target_temperature,
|
||||
set_fn=lambda client, device_id, value: client.set_temperature(device_id, value),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class MyNumber(MyEntity, NumberEntity):
|
||||
"""Number using entity description."""
|
||||
|
||||
entity_description: MyNumberDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: MyCoordinator,
|
||||
device_id: str,
|
||||
description: MyNumberDescription,
|
||||
) -> None:
|
||||
"""Initialize the number."""
|
||||
super().__init__(coordinator, device_id)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{device_id}_{description.key}"
|
||||
|
||||
@property
|
||||
def native_value(self) -> float | None:
|
||||
"""Return current value."""
|
||||
if device := self.coordinator.data.devices.get(self.device_id):
|
||||
return self.entity_description.value_fn(device)
|
||||
return None
|
||||
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Set new value."""
|
||||
await self.entity_description.set_fn(
|
||||
self.coordinator.client,
|
||||
self.device_id,
|
||||
value,
|
||||
)
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
## Value Validation
|
||||
|
||||
Home Assistant validates against min/max/step, but you can add custom validation:
|
||||
|
||||
```python
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Set value with custom validation."""
|
||||
# Custom validation
|
||||
if value % 5 != 0:
|
||||
raise ValueError("Value must be multiple of 5")
|
||||
|
||||
await self.device.set_value(value)
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
## State Update Patterns
|
||||
|
||||
### Pattern 1: Optimistic Update
|
||||
|
||||
```python
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Set value with optimistic update."""
|
||||
# Update immediately
|
||||
self._attr_native_value = value
|
||||
self.async_write_ha_state()
|
||||
|
||||
try:
|
||||
await self.device.set_value(value)
|
||||
except DeviceError:
|
||||
# Revert on error
|
||||
await self.coordinator.async_request_refresh()
|
||||
raise
|
||||
```
|
||||
|
||||
### Pattern 2: Coordinator Refresh
|
||||
|
||||
```python
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Set value and refresh."""
|
||||
await self.device.set_value(value)
|
||||
# Get actual value from device
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
### Pattern 3: Direct State Update
|
||||
|
||||
```python
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Set value with direct state update."""
|
||||
new_value = await self.device.set_value(value)
|
||||
# Device returns actual value
|
||||
self._attr_native_value = new_value
|
||||
self.async_write_ha_state()
|
||||
```
|
||||
|
||||
## Testing Numbers
|
||||
|
||||
### Snapshot Testing
|
||||
|
||||
```python
|
||||
"""Test numbers."""
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_numbers(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
init_integration,
|
||||
) -> None:
|
||||
"""Test number entities."""
|
||||
await snapshot_platform(
|
||||
hass,
|
||||
entity_registry,
|
||||
snapshot,
|
||||
mock_config_entry.entry_id,
|
||||
)
|
||||
```
|
||||
|
||||
### Value Testing
|
||||
|
||||
```python
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.components.number import (
|
||||
ATTR_VALUE,
|
||||
DOMAIN as NUMBER_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
)
|
||||
|
||||
|
||||
async def test_set_value(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_device,
|
||||
) -> None:
|
||||
"""Test setting number value."""
|
||||
await init_integration(hass, mock_config_entry)
|
||||
|
||||
# Check initial value
|
||||
state = hass.states.get("number.my_device_volume")
|
||||
assert state
|
||||
assert state.state == "50"
|
||||
assert state.attributes["min"] == 0
|
||||
assert state.attributes["max"] == 100
|
||||
assert state.attributes["step"] == 1
|
||||
|
||||
# Set new value
|
||||
await hass.services.async_call(
|
||||
NUMBER_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
{
|
||||
ATTR_ENTITY_ID: "number.my_device_volume",
|
||||
ATTR_VALUE: 75,
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
mock_device.set_volume.assert_called_once_with(75)
|
||||
|
||||
# Verify state updated
|
||||
state = hass.states.get("number.my_device_volume")
|
||||
assert state.state == "75"
|
||||
```
|
||||
|
||||
## Common Number Types
|
||||
|
||||
### Volume Control
|
||||
|
||||
```python
|
||||
class VolumeNumber(NumberEntity):
|
||||
"""Volume control."""
|
||||
|
||||
_attr_native_min_value = 0
|
||||
_attr_native_max_value = 100
|
||||
_attr_native_step = 1
|
||||
_attr_native_unit_of_measurement = PERCENTAGE
|
||||
_attr_mode = NumberMode.SLIDER
|
||||
```
|
||||
|
||||
### Temperature Setpoint
|
||||
|
||||
```python
|
||||
class TemperatureNumber(NumberEntity):
|
||||
"""Temperature setpoint."""
|
||||
|
||||
_attr_device_class = NumberDeviceClass.TEMPERATURE
|
||||
_attr_native_min_value = 16.0
|
||||
_attr_native_max_value = 30.0
|
||||
_attr_native_step = 0.5
|
||||
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
|
||||
_attr_mode = NumberMode.SLIDER
|
||||
```
|
||||
|
||||
### Duration Setting
|
||||
|
||||
```python
|
||||
class DurationNumber(NumberEntity):
|
||||
"""Duration setting."""
|
||||
|
||||
_attr_device_class = NumberDeviceClass.DURATION
|
||||
_attr_native_min_value = 0
|
||||
_attr_native_max_value = 3600
|
||||
_attr_native_step = 60 # 1 minute steps
|
||||
_attr_native_unit_of_measurement = UnitOfTime.SECONDS
|
||||
_attr_mode = NumberMode.BOX
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### ✅ DO
|
||||
|
||||
- Set appropriate min/max/step values
|
||||
- Use device class when available
|
||||
- Use standard units
|
||||
- Set display mode appropriately
|
||||
- Implement unique IDs
|
||||
- Use translation keys
|
||||
- Mark config numbers with entity_category
|
||||
- Handle value updates properly
|
||||
|
||||
### ❌ DON'T
|
||||
|
||||
- Allow invalid ranges (min > max)
|
||||
- Use zero or negative step
|
||||
- Block the event loop
|
||||
- Ignore validation errors
|
||||
- Create numbers without min/max/step
|
||||
- Hardcode entity names
|
||||
- Use for binary values (use switch)
|
||||
- Use for selection from list (use select)
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Number Not Appearing
|
||||
|
||||
Check:
|
||||
- [ ] Unique ID is set
|
||||
- [ ] Platform is in PLATFORMS list
|
||||
- [ ] min/max/step are all set
|
||||
- [ ] Entity is added with async_add_entities
|
||||
|
||||
### Value Not Updating
|
||||
|
||||
Check:
|
||||
- [ ] async_set_native_value is called
|
||||
- [ ] Coordinator refresh is working
|
||||
- [ ] native_value returns correct value
|
||||
- [ ] Value is within min/max range
|
||||
|
||||
### UI Shows Wrong Control Type
|
||||
|
||||
Check:
|
||||
- [ ] mode is set correctly
|
||||
- [ ] Range is appropriate for mode
|
||||
- [ ] Step size is reasonable
|
||||
|
||||
## Quality Scale Considerations
|
||||
|
||||
- **Bronze**: Unique ID required
|
||||
- **Gold**: Entity translations, device class, entity category
|
||||
- **Platinum**: Full type hints
|
||||
|
||||
## References
|
||||
|
||||
- [Number Documentation](https://developers.home-assistant.io/docs/core/entity/number)
|
||||
- [Number Integration](https://www.home-assistant.io/integrations/number/)
|
||||
@@ -1,520 +0,0 @@
|
||||
# Select Platform Reference
|
||||
|
||||
## Overview
|
||||
|
||||
Select entities allow users to choose from a predefined list of options. They're used for settings like operation modes, presets, input sources, or any configuration with a fixed set of choices.
|
||||
|
||||
## Basic Select Implementation
|
||||
|
||||
```python
|
||||
"""Select platform for my_integration."""
|
||||
from homeassistant.components.select import SelectEntity
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import MyConfigEntry
|
||||
from .coordinator import MyCoordinator
|
||||
from .entity import MyEntity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: MyConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up selects."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
MySelect(coordinator, device_id)
|
||||
for device_id in coordinator.data.devices
|
||||
)
|
||||
|
||||
|
||||
class MySelect(MyEntity, SelectEntity):
|
||||
"""Representation of a select."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
_attr_translation_key = "operation_mode"
|
||||
_attr_options = ["auto", "cool", "heat", "fan"]
|
||||
|
||||
def __init__(self, coordinator: MyCoordinator, device_id: str) -> None:
|
||||
"""Initialize the select."""
|
||||
super().__init__(coordinator, device_id)
|
||||
self._attr_unique_id = f"{device_id}_mode"
|
||||
|
||||
@property
|
||||
def current_option(self) -> str | None:
|
||||
"""Return the current selected option."""
|
||||
if device := self.coordinator.data.devices.get(self.device_id):
|
||||
return device.mode
|
||||
return None
|
||||
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Change the selected option."""
|
||||
await self.coordinator.client.set_mode(self.device_id, option)
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
## Select Properties
|
||||
|
||||
### Core Properties
|
||||
|
||||
```python
|
||||
class MySelect(SelectEntity):
|
||||
"""Select with all common properties."""
|
||||
|
||||
# Basic identification
|
||||
_attr_has_entity_name = True
|
||||
_attr_translation_key = "preset"
|
||||
_attr_unique_id = "device_123_preset"
|
||||
|
||||
# Available options (required)
|
||||
_attr_options = ["comfort", "eco", "away", "sleep"]
|
||||
|
||||
# Entity category
|
||||
_attr_entity_category = EntityCategory.CONFIG
|
||||
|
||||
@property
|
||||
def current_option(self) -> str | None:
|
||||
"""Return current selected option."""
|
||||
return self.device.preset
|
||||
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Set the selected option."""
|
||||
await self.device.set_preset(option)
|
||||
```
|
||||
|
||||
### Required Properties and Methods
|
||||
|
||||
```python
|
||||
# List of available options
|
||||
_attr_options = ["option1", "option2", "option3"]
|
||||
|
||||
# Current selected option
|
||||
@property
|
||||
def current_option(self) -> str | None:
|
||||
"""Return the selected option."""
|
||||
return self.device.current_mode
|
||||
|
||||
# Or use attribute
|
||||
_attr_current_option = "option1"
|
||||
|
||||
# Method to change option
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Change the selected option."""
|
||||
await self.device.set_option(option)
|
||||
```
|
||||
|
||||
## Using Enums for Options
|
||||
|
||||
Recommended pattern for type safety:
|
||||
|
||||
```python
|
||||
from enum import StrEnum
|
||||
|
||||
|
||||
class OperationMode(StrEnum):
|
||||
"""Operation modes."""
|
||||
AUTO = "auto"
|
||||
COOL = "cool"
|
||||
HEAT = "heat"
|
||||
FAN = "fan"
|
||||
|
||||
|
||||
class MySelect(SelectEntity):
|
||||
"""Select using enum."""
|
||||
|
||||
_attr_options = [mode.value for mode in OperationMode]
|
||||
|
||||
@property
|
||||
def current_option(self) -> str | None:
|
||||
"""Return current mode."""
|
||||
if device := self.coordinator.data.devices.get(self.device_id):
|
||||
return device.mode
|
||||
return None
|
||||
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Set mode."""
|
||||
# Validate option is in enum
|
||||
mode = OperationMode(option)
|
||||
await self.coordinator.client.set_mode(self.device_id, mode)
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
## Entity Descriptions Pattern
|
||||
|
||||
For multiple select entities:
|
||||
|
||||
```python
|
||||
from dataclasses import dataclass
|
||||
from collections.abc import Awaitable, Callable
|
||||
|
||||
from homeassistant.components.select import SelectEntityDescription
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class MySelectDescription(SelectEntityDescription):
|
||||
"""Describes a select."""
|
||||
current_fn: Callable[[MyData], str | None]
|
||||
select_fn: Callable[[MyClient, str, str], Awaitable[None]]
|
||||
|
||||
|
||||
SELECTS: tuple[MySelectDescription, ...] = (
|
||||
MySelectDescription(
|
||||
key="mode",
|
||||
translation_key="operation_mode",
|
||||
options=["auto", "cool", "heat", "fan"],
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
current_fn=lambda data: data.mode,
|
||||
select_fn=lambda client, device_id, option: client.set_mode(device_id, option),
|
||||
),
|
||||
MySelectDescription(
|
||||
key="preset",
|
||||
translation_key="preset",
|
||||
options=["comfort", "eco", "away", "sleep"],
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
current_fn=lambda data: data.preset,
|
||||
select_fn=lambda client, device_id, option: client.set_preset(device_id, option),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: MyConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up selects."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
MySelect(coordinator, device_id, description)
|
||||
for device_id in coordinator.data.devices
|
||||
for description in SELECTS
|
||||
)
|
||||
|
||||
|
||||
class MySelect(MyEntity, SelectEntity):
|
||||
"""Select using entity description."""
|
||||
|
||||
entity_description: MySelectDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: MyCoordinator,
|
||||
device_id: str,
|
||||
description: MySelectDescription,
|
||||
) -> None:
|
||||
"""Initialize the select."""
|
||||
super().__init__(coordinator, device_id)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{device_id}_{description.key}"
|
||||
|
||||
@property
|
||||
def current_option(self) -> str | None:
|
||||
"""Return current option."""
|
||||
if device := self.coordinator.data.devices.get(self.device_id):
|
||||
return self.entity_description.current_fn(device)
|
||||
return None
|
||||
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Select option."""
|
||||
await self.entity_description.select_fn(
|
||||
self.coordinator.client,
|
||||
self.device_id,
|
||||
option,
|
||||
)
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
## Dynamic Options
|
||||
|
||||
If options change based on device state:
|
||||
|
||||
```python
|
||||
class MyDynamicSelect(SelectEntity):
|
||||
"""Select with dynamic options."""
|
||||
|
||||
@property
|
||||
def options(self) -> list[str]:
|
||||
"""Return available options based on device state."""
|
||||
if device := self.coordinator.data.devices.get(self.device_id):
|
||||
return device.available_modes
|
||||
return []
|
||||
|
||||
@property
|
||||
def current_option(self) -> str | None:
|
||||
"""Return current option."""
|
||||
if device := self.coordinator.data.devices.get(self.device_id):
|
||||
return device.current_mode
|
||||
return None
|
||||
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Select option."""
|
||||
await self.device.set_mode(option)
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
## Option Translation
|
||||
|
||||
Use translation keys for user-friendly option labels:
|
||||
|
||||
```json
|
||||
// strings.json
|
||||
{
|
||||
"entity": {
|
||||
"select": {
|
||||
"operation_mode": {
|
||||
"name": "Operation mode",
|
||||
"state": {
|
||||
"auto": "Automatic",
|
||||
"cool": "Cooling",
|
||||
"heat": "Heating",
|
||||
"fan": "Fan only"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```python
|
||||
class MySelect(SelectEntity):
|
||||
"""Select with translated options."""
|
||||
|
||||
_attr_translation_key = "operation_mode"
|
||||
_attr_options = ["auto", "cool", "heat", "fan"]
|
||||
```
|
||||
|
||||
## State Update Patterns
|
||||
|
||||
### Pattern 1: Optimistic Update
|
||||
|
||||
```python
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Select option with optimistic update."""
|
||||
# Update immediately
|
||||
self._attr_current_option = option
|
||||
self.async_write_ha_state()
|
||||
|
||||
try:
|
||||
await self.device.set_option(option)
|
||||
except DeviceError:
|
||||
# Revert on error
|
||||
await self.coordinator.async_request_refresh()
|
||||
raise
|
||||
```
|
||||
|
||||
### Pattern 2: Coordinator Refresh
|
||||
|
||||
```python
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Select option and refresh."""
|
||||
await self.device.set_option(option)
|
||||
# Get actual option from device
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
### Pattern 3: Direct State Update
|
||||
|
||||
```python
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Select option with direct state update."""
|
||||
actual_option = await self.device.set_option(option)
|
||||
# Device returns actual option
|
||||
self._attr_current_option = actual_option
|
||||
self.async_write_ha_state()
|
||||
```
|
||||
|
||||
## Testing Selects
|
||||
|
||||
### Snapshot Testing
|
||||
|
||||
```python
|
||||
"""Test selects."""
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_selects(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
init_integration,
|
||||
) -> None:
|
||||
"""Test select entities."""
|
||||
await snapshot_platform(
|
||||
hass,
|
||||
entity_registry,
|
||||
snapshot,
|
||||
mock_config_entry.entry_id,
|
||||
)
|
||||
```
|
||||
|
||||
### Option Selection Testing
|
||||
|
||||
```python
|
||||
from homeassistant.const import ATTR_ENTITY_ID, ATTR_OPTION
|
||||
from homeassistant.components.select import DOMAIN as SELECT_DOMAIN, SERVICE_SELECT_OPTION
|
||||
|
||||
|
||||
async def test_select_option(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_device,
|
||||
) -> None:
|
||||
"""Test selecting an option."""
|
||||
await init_integration(hass, mock_config_entry)
|
||||
|
||||
# Check initial state
|
||||
state = hass.states.get("select.my_device_mode")
|
||||
assert state
|
||||
assert state.state == "auto"
|
||||
assert state.attributes["options"] == ["auto", "cool", "heat", "fan"]
|
||||
|
||||
# Select new option
|
||||
await hass.services.async_call(
|
||||
SELECT_DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
{
|
||||
ATTR_ENTITY_ID: "select.my_device_mode",
|
||||
ATTR_OPTION: "cool",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
mock_device.set_mode.assert_called_once_with("cool")
|
||||
|
||||
# Verify state updated
|
||||
state = hass.states.get("select.my_device_mode")
|
||||
assert state.state == "cool"
|
||||
```
|
||||
|
||||
## Common Select Types
|
||||
|
||||
### Operation Mode
|
||||
|
||||
```python
|
||||
class ModeSelect(SelectEntity):
|
||||
"""Operation mode select."""
|
||||
|
||||
_attr_translation_key = "operation_mode"
|
||||
_attr_options = ["auto", "cool", "heat", "fan", "dry"]
|
||||
_attr_entity_category = EntityCategory.CONFIG
|
||||
```
|
||||
|
||||
### Preset
|
||||
|
||||
```python
|
||||
class PresetSelect(SelectEntity):
|
||||
"""Preset select."""
|
||||
|
||||
_attr_translation_key = "preset"
|
||||
_attr_options = ["comfort", "eco", "away", "sleep", "boost"]
|
||||
_attr_entity_category = EntityCategory.CONFIG
|
||||
```
|
||||
|
||||
### Input Source
|
||||
|
||||
```python
|
||||
class InputSourceSelect(SelectEntity):
|
||||
"""Input source select."""
|
||||
|
||||
_attr_translation_key = "source"
|
||||
_attr_options = ["hdmi1", "hdmi2", "usb", "bluetooth", "optical"]
|
||||
```
|
||||
|
||||
### Effect/Scene
|
||||
|
||||
```python
|
||||
class EffectSelect(SelectEntity):
|
||||
"""Light effect select."""
|
||||
|
||||
_attr_translation_key = "effect"
|
||||
_attr_options = ["none", "rainbow", "pulse", "strobe", "breathe"]
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### ✅ DO
|
||||
|
||||
- Use enums for type safety
|
||||
- Provide translation keys for options
|
||||
- Validate selected options
|
||||
- Implement unique IDs
|
||||
- Use entity_category for config selects
|
||||
- Keep option lists reasonable (<20 items)
|
||||
- Use consistent option naming (lowercase, underscores)
|
||||
- Provide clear option translations
|
||||
|
||||
### ❌ DON'T
|
||||
|
||||
- Accept options not in the list
|
||||
- Have too many options (use input_select helper instead)
|
||||
- Block the event loop
|
||||
- Hardcode entity names
|
||||
- Change options list arbitrarily
|
||||
- Use for numeric values (use number entity)
|
||||
- Use for binary choices (use switch)
|
||||
- Have empty options list
|
||||
|
||||
## Select vs. Other Entities
|
||||
|
||||
**Use Select when**:
|
||||
- Fixed list of text options
|
||||
- Modes, presets, or settings
|
||||
- 2-20 options
|
||||
|
||||
**Use Switch when**:
|
||||
- Binary on/off control
|
||||
- Only 2 states
|
||||
|
||||
**Use Number when**:
|
||||
- Numeric range
|
||||
- Continuous values
|
||||
|
||||
**Use Input Select when**:
|
||||
- User-defined options
|
||||
- Need dynamic option list
|
||||
- Helper/template integration
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Select Not Appearing
|
||||
|
||||
Check:
|
||||
- [ ] Unique ID is set
|
||||
- [ ] Platform is in PLATFORMS list
|
||||
- [ ] options list is not empty
|
||||
- [ ] Entity is added with async_add_entities
|
||||
|
||||
### Option Not Accepted
|
||||
|
||||
Check:
|
||||
- [ ] Option is in options list (case-sensitive)
|
||||
- [ ] Options list is properly formatted
|
||||
- [ ] async_select_option handles the option
|
||||
|
||||
### Options Not Translating
|
||||
|
||||
Check:
|
||||
- [ ] translation_key is set
|
||||
- [ ] strings.json has state translations
|
||||
- [ ] Option keys match exactly
|
||||
|
||||
## Quality Scale Considerations
|
||||
|
||||
- **Bronze**: Unique ID required
|
||||
- **Gold**: Entity translations, entity category
|
||||
- **Platinum**: Full type hints, use StrEnum for options
|
||||
|
||||
## References
|
||||
|
||||
- [Select Documentation](https://developers.home-assistant.io/docs/core/entity/select)
|
||||
- [Select Integration](https://www.home-assistant.io/integrations/select/)
|
||||
@@ -1,560 +0,0 @@
|
||||
# Sensor Platform Reference
|
||||
|
||||
## Overview
|
||||
|
||||
Sensors are read-only entities that represent measurements, states, or information from devices and services. They display numeric values, strings, timestamps, or other data types.
|
||||
|
||||
## Basic Sensor Implementation
|
||||
|
||||
### Minimal Sensor
|
||||
|
||||
```python
|
||||
"""Sensor platform for my_integration."""
|
||||
from homeassistant.components.sensor import SensorEntity
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import MyConfigEntry
|
||||
from .const import DOMAIN
|
||||
from .coordinator import MyCoordinator
|
||||
from .entity import MyEntity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: MyConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up sensors."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
MySensor(coordinator, device_id)
|
||||
for device_id in coordinator.data.devices
|
||||
)
|
||||
|
||||
|
||||
class MySensor(MyEntity, SensorEntity):
|
||||
"""Representation of a sensor."""
|
||||
|
||||
def __init__(self, coordinator: MyCoordinator, device_id: str) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(coordinator, device_id)
|
||||
self._attr_unique_id = f"{device_id}_temperature"
|
||||
self._attr_translation_key = "temperature"
|
||||
|
||||
@property
|
||||
def native_value(self) -> float | None:
|
||||
"""Return the sensor value."""
|
||||
if device := self.coordinator.data.devices.get(self.device_id):
|
||||
return device.temperature
|
||||
return None
|
||||
```
|
||||
|
||||
## Sensor Properties
|
||||
|
||||
### Core Properties
|
||||
|
||||
```python
|
||||
class MySensor(SensorEntity):
|
||||
"""Sensor with all common properties."""
|
||||
|
||||
# Basic identification
|
||||
_attr_has_entity_name = True # Required
|
||||
_attr_translation_key = "temperature" # For translations
|
||||
_attr_unique_id = "device_123_temp" # Required
|
||||
|
||||
# Device class and units
|
||||
_attr_device_class = SensorDeviceClass.TEMPERATURE
|
||||
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
|
||||
_attr_suggested_display_precision = 1 # Decimal places
|
||||
|
||||
# State class for statistics
|
||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||
|
||||
# Entity category
|
||||
_attr_entity_category = EntityCategory.DIAGNOSTIC # If diagnostic
|
||||
|
||||
# Availability
|
||||
_attr_entity_registry_enabled_default = False # If noisy/less important
|
||||
|
||||
@property
|
||||
def native_value(self) -> float | None:
|
||||
"""Return sensor value."""
|
||||
return self.device.temperature
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available."""
|
||||
return super().available and self.device_id in self.coordinator.data
|
||||
```
|
||||
|
||||
## Device Classes
|
||||
|
||||
Use device classes for proper representation:
|
||||
|
||||
```python
|
||||
from homeassistant.components.sensor import SensorDeviceClass
|
||||
|
||||
# Common device classes
|
||||
_attr_device_class = SensorDeviceClass.TEMPERATURE
|
||||
_attr_device_class = SensorDeviceClass.HUMIDITY
|
||||
_attr_device_class = SensorDeviceClass.PRESSURE
|
||||
_attr_device_class = SensorDeviceClass.BATTERY
|
||||
_attr_device_class = SensorDeviceClass.ENERGY
|
||||
_attr_device_class = SensorDeviceClass.POWER
|
||||
_attr_device_class = SensorDeviceClass.VOLTAGE
|
||||
_attr_device_class = SensorDeviceClass.CURRENT
|
||||
_attr_device_class = SensorDeviceClass.TIMESTAMP
|
||||
_attr_device_class = SensorDeviceClass.MONETARY
|
||||
```
|
||||
|
||||
Benefits:
|
||||
- Automatic unit conversion
|
||||
- Proper UI representation
|
||||
- Voice assistant integration
|
||||
- Historical statistics
|
||||
|
||||
## State Classes
|
||||
|
||||
For long-term statistics support:
|
||||
|
||||
```python
|
||||
from homeassistant.components.sensor import SensorStateClass
|
||||
|
||||
# Measurement - value at a point in time
|
||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||
# Examples: temperature, humidity, power
|
||||
|
||||
# Total - cumulative value that can increase/decrease
|
||||
_attr_state_class = SensorStateClass.TOTAL
|
||||
# Examples: energy consumed, data transferred
|
||||
# Use with last_reset for resettable totals
|
||||
|
||||
# Total increasing - cumulative value that only increases
|
||||
_attr_state_class = SensorStateClass.TOTAL_INCREASING
|
||||
# Examples: lifetime energy, odometer
|
||||
```
|
||||
|
||||
### When to Use State Classes
|
||||
|
||||
✅ **Use MEASUREMENT for**:
|
||||
- Temperature, humidity, pressure
|
||||
- Current power usage
|
||||
- Instantaneous values
|
||||
|
||||
✅ **Use TOTAL for**:
|
||||
- Daily/monthly energy consumption (resets)
|
||||
- Periodic counters
|
||||
|
||||
✅ **Use TOTAL_INCREASING for**:
|
||||
- Lifetime energy consumption
|
||||
- Monotonically increasing counters
|
||||
|
||||
❌ **Don't use state class for**:
|
||||
- Text/string sensors
|
||||
- Status sensors (enum values)
|
||||
- Non-numeric sensors
|
||||
|
||||
## Unit of Measurement
|
||||
|
||||
### Using Standard Units
|
||||
|
||||
```python
|
||||
from homeassistant.const import (
|
||||
UnitOfTemperature,
|
||||
UnitOfPower,
|
||||
UnitOfEnergy,
|
||||
PERCENTAGE,
|
||||
)
|
||||
|
||||
# Temperature
|
||||
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
|
||||
# Auto-converts to user's preference (°F/°C/K)
|
||||
|
||||
# Power
|
||||
_attr_native_unit_of_measurement = UnitOfPower.WATT
|
||||
|
||||
# Energy
|
||||
_attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
|
||||
|
||||
# Percentage
|
||||
_attr_native_unit_of_measurement = PERCENTAGE
|
||||
```
|
||||
|
||||
### Custom Units
|
||||
|
||||
```python
|
||||
# For non-standard units
|
||||
_attr_native_unit_of_measurement = "AQI" # Air Quality Index
|
||||
_attr_native_unit_of_measurement = "ppm" # Parts per million
|
||||
```
|
||||
|
||||
## Entity Descriptions Pattern
|
||||
|
||||
For multiple similar sensors, use SensorEntityDescription:
|
||||
|
||||
```python
|
||||
from dataclasses import dataclass
|
||||
from homeassistant.components.sensor import SensorEntityDescription
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class MySensorDescription(SensorEntityDescription):
|
||||
"""Describes a sensor."""
|
||||
value_fn: Callable[[MyData], StateType]
|
||||
|
||||
|
||||
SENSORS: tuple[MySensorDescription, ...] = (
|
||||
MySensorDescription(
|
||||
key="temperature",
|
||||
translation_key="temperature",
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
value_fn=lambda data: data.temperature,
|
||||
),
|
||||
MySensorDescription(
|
||||
key="humidity",
|
||||
translation_key="humidity",
|
||||
device_class=SensorDeviceClass.HUMIDITY,
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
value_fn=lambda data: data.humidity,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class MySensor(MyEntity, SensorEntity):
|
||||
"""Sensor using entity description."""
|
||||
|
||||
entity_description: MySensorDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: MyCoordinator,
|
||||
description: MySensorDescription,
|
||||
device_id: str,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(coordinator, device_id)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{device_id}_{description.key}"
|
||||
|
||||
@property
|
||||
def native_value(self) -> StateType:
|
||||
"""Return the sensor value."""
|
||||
if device := self.coordinator.data.devices.get(self.device_id):
|
||||
return self.entity_description.value_fn(device)
|
||||
return None
|
||||
```
|
||||
|
||||
### Lambda Functions in EntityDescription
|
||||
|
||||
When lambdas get long, use proper formatting:
|
||||
|
||||
```python
|
||||
# ❌ Bad - too long
|
||||
SensorEntityDescription(
|
||||
key="temperature",
|
||||
value_fn=lambda data: round(data["temp_value"] * 1.8 + 32, 1) if data.get("temp_value") is not None else None,
|
||||
)
|
||||
|
||||
# ✅ Good - wrapped properly
|
||||
SensorEntityDescription(
|
||||
key="temperature",
|
||||
value_fn=lambda data: (
|
||||
round(data["temp_value"] * 1.8 + 32, 1)
|
||||
if data.get("temp_value") is not None
|
||||
else None
|
||||
),
|
||||
)
|
||||
```
|
||||
|
||||
## Timestamp Sensors
|
||||
|
||||
For datetime values:
|
||||
|
||||
```python
|
||||
from datetime import datetime
|
||||
from homeassistant.components.sensor import SensorDeviceClass
|
||||
|
||||
class MyTimestampSensor(SensorEntity):
|
||||
"""Timestamp sensor."""
|
||||
|
||||
_attr_device_class = SensorDeviceClass.TIMESTAMP
|
||||
|
||||
@property
|
||||
def native_value(self) -> datetime | None:
|
||||
"""Return timestamp."""
|
||||
return self.device.last_update
|
||||
```
|
||||
|
||||
## Enum Sensors
|
||||
|
||||
For sensors with fixed set of possible values:
|
||||
|
||||
```python
|
||||
from enum import StrEnum
|
||||
from homeassistant.components.sensor import SensorEntity
|
||||
|
||||
class OperationMode(StrEnum):
|
||||
"""Operation modes."""
|
||||
AUTO = "auto"
|
||||
MANUAL = "manual"
|
||||
ECO = "eco"
|
||||
|
||||
|
||||
class MyModeSensor(SensorEntity):
|
||||
"""Mode sensor."""
|
||||
|
||||
_attr_device_class = SensorDeviceClass.ENUM
|
||||
_attr_options = [mode.value for mode in OperationMode]
|
||||
|
||||
@property
|
||||
def native_value(self) -> str | None:
|
||||
"""Return current mode."""
|
||||
return self.device.mode
|
||||
```
|
||||
|
||||
## Entity Category
|
||||
|
||||
Mark diagnostic or configuration sensors:
|
||||
|
||||
```python
|
||||
from homeassistant.helpers.entity import EntityCategory
|
||||
|
||||
# Diagnostic sensors (technical info)
|
||||
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
||||
# Examples: signal strength, uptime, IP address
|
||||
|
||||
# Config sensors (device settings)
|
||||
_attr_entity_category = EntityCategory.CONFIG
|
||||
# Examples: current mode setting, configuration values
|
||||
```
|
||||
|
||||
## Disabled by Default
|
||||
|
||||
For noisy or less important sensors:
|
||||
|
||||
```python
|
||||
class MySignalStrengthSensor(SensorEntity):
|
||||
"""Signal strength sensor - noisy."""
|
||||
|
||||
_attr_entity_registry_enabled_default = False
|
||||
```
|
||||
|
||||
## Dynamic Sensor Addition
|
||||
|
||||
For devices that appear after setup:
|
||||
|
||||
```python
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: MyConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up sensors with dynamic addition."""
|
||||
coordinator = entry.runtime_data
|
||||
known_devices: set[str] = set()
|
||||
|
||||
@callback
|
||||
def _add_new_devices() -> None:
|
||||
"""Add newly discovered devices."""
|
||||
current_devices = set(coordinator.data.devices.keys())
|
||||
new_devices = current_devices - known_devices
|
||||
|
||||
if new_devices:
|
||||
known_devices.update(new_devices)
|
||||
async_add_entities(
|
||||
MySensor(coordinator, device_id)
|
||||
for device_id in new_devices
|
||||
)
|
||||
|
||||
# Initial setup
|
||||
_add_new_devices()
|
||||
|
||||
# Listen for new devices
|
||||
entry.async_on_unload(coordinator.async_add_listener(_add_new_devices))
|
||||
```
|
||||
|
||||
## Testing Sensors
|
||||
|
||||
### Test with Snapshots
|
||||
|
||||
```python
|
||||
"""Test sensors."""
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_sensors(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
init_integration,
|
||||
) -> None:
|
||||
"""Test sensor entities."""
|
||||
await snapshot_platform(
|
||||
hass,
|
||||
entity_registry,
|
||||
snapshot,
|
||||
mock_config_entry.entry_id,
|
||||
)
|
||||
```
|
||||
|
||||
### Test Sensor Values
|
||||
|
||||
```python
|
||||
async def test_sensor_values(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test sensor values are correct."""
|
||||
await init_integration(hass, mock_config_entry)
|
||||
|
||||
state = hass.states.get("sensor.my_device_temperature")
|
||||
assert state
|
||||
assert state.state == "22.5"
|
||||
assert state.attributes["unit_of_measurement"] == "°C"
|
||||
assert state.attributes["device_class"] == "temperature"
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### ✅ DO
|
||||
|
||||
- Use device classes when available
|
||||
- Set state classes for statistics
|
||||
- Use standard units of measurement
|
||||
- Implement unique IDs
|
||||
- Use entity descriptions for similar sensors
|
||||
- Mark diagnostic sensors with entity_category
|
||||
- Disable noisy sensors by default
|
||||
- Return None for unknown values
|
||||
- Use translation keys for entity names
|
||||
|
||||
### ❌ DON'T
|
||||
|
||||
- Hardcode entity names
|
||||
- Use string "unavailable" or "unknown" as state
|
||||
- Mix units (always use native_unit_of_measurement)
|
||||
- Create sensors without unique IDs
|
||||
- Poll in sensor update if using coordinator
|
||||
- Block the event loop
|
||||
- Use state class for non-numeric sensors
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Pattern 1: Coordinator-Based Sensor
|
||||
|
||||
```python
|
||||
class MySensor(CoordinatorEntity[MyCoordinator], SensorEntity):
|
||||
"""Coordinator-based sensor."""
|
||||
|
||||
_attr_should_poll = False # Coordinator handles updates
|
||||
|
||||
@property
|
||||
def native_value(self) -> StateType:
|
||||
"""Get value from coordinator data."""
|
||||
return self.coordinator.data.get(self.key)
|
||||
```
|
||||
|
||||
### Pattern 2: Push-Updated Sensor
|
||||
|
||||
```python
|
||||
class MyPushSensor(SensorEntity):
|
||||
"""Push-updated sensor."""
|
||||
|
||||
_attr_should_poll = False
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Subscribe to updates."""
|
||||
self.async_on_remove(
|
||||
self.device.subscribe(self._handle_update)
|
||||
)
|
||||
|
||||
@callback
|
||||
def _handle_update(self, value: float) -> None:
|
||||
"""Handle push update."""
|
||||
self._attr_native_value = value
|
||||
self.async_write_ha_state()
|
||||
```
|
||||
|
||||
### Pattern 3: Calculated Sensor
|
||||
|
||||
```python
|
||||
class MyCalculatedSensor(SensorEntity):
|
||||
"""Calculated from other sensors."""
|
||||
|
||||
_attr_should_poll = False
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Subscribe to source sensors."""
|
||||
self.async_on_remove(
|
||||
async_track_state_change_event(
|
||||
self.hass,
|
||||
["sensor.source1", "sensor.source2"],
|
||||
self._handle_update,
|
||||
)
|
||||
)
|
||||
|
||||
@callback
|
||||
def _handle_update(self, event: Event) -> None:
|
||||
"""Recalculate when sources change."""
|
||||
# Calculate new value
|
||||
self._attr_native_value = self._calculate()
|
||||
self.async_write_ha_state()
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Sensor Not Appearing
|
||||
|
||||
Check:
|
||||
- [ ] Unique ID is set
|
||||
- [ ] Platform is in PLATFORMS list
|
||||
- [ ] async_setup_entry is called
|
||||
- [ ] Entity is added with async_add_entities
|
||||
|
||||
### Values Not Updating
|
||||
|
||||
Check:
|
||||
- [ ] Coordinator is updating
|
||||
- [ ] Entity is available
|
||||
- [ ] native_value returns correct data
|
||||
- [ ] should_poll is False for coordinator
|
||||
|
||||
### Units Not Converting
|
||||
|
||||
Check:
|
||||
- [ ] Using standard unit constants
|
||||
- [ ] Device class is set correctly
|
||||
- [ ] Unit matches device class
|
||||
|
||||
### Statistics Not Working
|
||||
|
||||
Check:
|
||||
- [ ] State class is set
|
||||
- [ ] Values are numeric
|
||||
- [ ] Device class is appropriate
|
||||
- [ ] Units are consistent
|
||||
|
||||
## Quality Scale Considerations
|
||||
|
||||
- **Bronze**: Unique ID required
|
||||
- **Gold**: Entity translations, device class, entity category
|
||||
- **Platinum**: Full type hints
|
||||
|
||||
## References
|
||||
|
||||
- [Sensor Documentation](https://developers.home-assistant.io/docs/core/entity/sensor)
|
||||
- [Device Classes](https://www.home-assistant.io/integrations/sensor/#device-class)
|
||||
- [State Classes](https://developers.home-assistant.io/docs/core/entity/sensor/#available-state-classes)
|
||||
@@ -1,505 +0,0 @@
|
||||
# Switch Platform Reference
|
||||
|
||||
## Overview
|
||||
|
||||
Switches are entities that can be turned on or off. They represent controllable devices like smart plugs, relays, or any binary control. Unlike binary sensors, switches can be controlled by the user.
|
||||
|
||||
## Basic Switch Implementation
|
||||
|
||||
```python
|
||||
"""Switch platform for my_integration."""
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import MyConfigEntry
|
||||
from .coordinator import MyCoordinator
|
||||
from .entity import MyEntity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: MyConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up switches."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
MySwitch(coordinator, device_id)
|
||||
for device_id in coordinator.data.devices
|
||||
)
|
||||
|
||||
|
||||
class MySwitch(MyEntity, SwitchEntity):
|
||||
"""Representation of a switch."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
_attr_translation_key = "outlet"
|
||||
|
||||
def __init__(self, coordinator: MyCoordinator, device_id: str) -> None:
|
||||
"""Initialize the switch."""
|
||||
super().__init__(coordinator, device_id)
|
||||
self._attr_unique_id = f"{device_id}_switch"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if switch is on."""
|
||||
if device := self.coordinator.data.devices.get(self.device_id):
|
||||
return device.is_on
|
||||
return None
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn the switch on."""
|
||||
await self.coordinator.client.turn_on(self.device_id)
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn the switch off."""
|
||||
await self.coordinator.client.turn_off(self.device_id)
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
## Switch Properties and Methods
|
||||
|
||||
### Core Properties
|
||||
|
||||
```python
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if entity is on."""
|
||||
return self.device.state
|
||||
|
||||
# Or use attribute
|
||||
_attr_is_on = True # or False, or None
|
||||
```
|
||||
|
||||
### Required Methods
|
||||
|
||||
```python
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn the entity on."""
|
||||
await self.device.turn_on()
|
||||
# Update state
|
||||
self._attr_is_on = True
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn the entity off."""
|
||||
await self.device.turn_off()
|
||||
# Update state
|
||||
self._attr_is_on = False
|
||||
self.async_write_ha_state()
|
||||
```
|
||||
|
||||
### Optional Toggle Method
|
||||
|
||||
```python
|
||||
async def async_toggle(self, **kwargs: Any) -> None:
|
||||
"""Toggle the entity."""
|
||||
# Only implement if device has native toggle
|
||||
await self.device.toggle()
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
**Note**: If `async_toggle` is not implemented, Home Assistant will use `async_turn_on`/`async_turn_off` based on current state.
|
||||
|
||||
## Device Class
|
||||
|
||||
Switches can have device classes to indicate their type:
|
||||
|
||||
```python
|
||||
from homeassistant.components.switch import SwitchDeviceClass
|
||||
|
||||
_attr_device_class = SwitchDeviceClass.OUTLET
|
||||
_attr_device_class = SwitchDeviceClass.SWITCH
|
||||
```
|
||||
|
||||
Device classes:
|
||||
- `OUTLET` - Smart plug/outlet
|
||||
- `SWITCH` - Generic switch (default)
|
||||
|
||||
## State Update Patterns
|
||||
|
||||
### Pattern 1: Optimistic Update
|
||||
|
||||
For fast UI response:
|
||||
|
||||
```python
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn on."""
|
||||
# Update state immediately (optimistic)
|
||||
self._attr_is_on = True
|
||||
self.async_write_ha_state()
|
||||
|
||||
try:
|
||||
await self.coordinator.client.turn_on(self.device_id)
|
||||
except DeviceError:
|
||||
# Revert on error
|
||||
self._attr_is_on = False
|
||||
self.async_write_ha_state()
|
||||
raise
|
||||
```
|
||||
|
||||
### Pattern 2: Coordinator Refresh
|
||||
|
||||
Wait for actual state:
|
||||
|
||||
```python
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn on."""
|
||||
await self.coordinator.client.turn_on(self.device_id)
|
||||
# Refresh coordinator to get actual state
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
### Pattern 3: Push Update
|
||||
|
||||
For push-based systems:
|
||||
|
||||
```python
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn on."""
|
||||
# Command device
|
||||
await self.device.turn_on()
|
||||
# State will be updated via push event
|
||||
# No need to call async_write_ha_state()
|
||||
```
|
||||
|
||||
## Entity Descriptions Pattern
|
||||
|
||||
For multiple similar switches:
|
||||
|
||||
```python
|
||||
from dataclasses import dataclass
|
||||
from collections.abc import Awaitable, Callable
|
||||
|
||||
from homeassistant.components.switch import SwitchEntityDescription
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class MySwitchDescription(SwitchEntityDescription):
|
||||
"""Describes a switch."""
|
||||
is_on_fn: Callable[[MyData], bool | None]
|
||||
turn_on_fn: Callable[[MyClient, str], Awaitable[None]]
|
||||
turn_off_fn: Callable[[MyClient, str], Awaitable[None]]
|
||||
|
||||
|
||||
SWITCHES: tuple[MySwitchDescription, ...] = (
|
||||
MySwitchDescription(
|
||||
key="outlet",
|
||||
translation_key="outlet",
|
||||
device_class=SwitchDeviceClass.OUTLET,
|
||||
is_on_fn=lambda data: data.outlet_state,
|
||||
turn_on_fn=lambda client, device_id: client.turn_on_outlet(device_id),
|
||||
turn_off_fn=lambda client, device_id: client.turn_off_outlet(device_id),
|
||||
),
|
||||
MySwitchDescription(
|
||||
key="led",
|
||||
translation_key="led",
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
is_on_fn=lambda data: data.led_enabled,
|
||||
turn_on_fn=lambda client, device_id: client.enable_led(device_id),
|
||||
turn_off_fn=lambda client, device_id: client.disable_led(device_id),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class MySwitch(MyEntity, SwitchEntity):
|
||||
"""Switch using entity description."""
|
||||
|
||||
entity_description: MySwitchDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: MyCoordinator,
|
||||
device_id: str,
|
||||
description: MySwitchDescription,
|
||||
) -> None:
|
||||
"""Initialize the switch."""
|
||||
super().__init__(coordinator, device_id)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{device_id}_{description.key}"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return if switch is on."""
|
||||
if device := self.coordinator.data.devices.get(self.device_id):
|
||||
return self.entity_description.is_on_fn(device)
|
||||
return None
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn on."""
|
||||
await self.entity_description.turn_on_fn(
|
||||
self.coordinator.client,
|
||||
self.device_id,
|
||||
)
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn off."""
|
||||
await self.entity_description.turn_off_fn(
|
||||
self.coordinator.client,
|
||||
self.device_id,
|
||||
)
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
## Configuration Switches
|
||||
|
||||
Switches that control device settings (not physical devices):
|
||||
|
||||
```python
|
||||
from homeassistant.helpers.entity import EntityCategory
|
||||
|
||||
class MyConfigSwitch(SwitchEntity):
|
||||
"""Configuration switch."""
|
||||
|
||||
_attr_entity_category = EntityCategory.CONFIG
|
||||
_attr_translation_key = "led_indicator"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return if LED is enabled."""
|
||||
return self.device.led_enabled
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Enable LED indicator."""
|
||||
await self.device.set_led(True)
|
||||
self._attr_is_on = True
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Disable LED indicator."""
|
||||
await self.device.set_led(False)
|
||||
self._attr_is_on = False
|
||||
self.async_write_ha_state()
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
Handle errors gracefully:
|
||||
|
||||
```python
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn on with error handling."""
|
||||
try:
|
||||
await self.device.turn_on()
|
||||
except DeviceOfflineError as err:
|
||||
# Let entity become unavailable
|
||||
raise HomeAssistantError(f"Device is offline: {err}") from err
|
||||
except DeviceError as err:
|
||||
# Specific error
|
||||
raise HomeAssistantError(f"Failed to turn on: {err}") from err
|
||||
else:
|
||||
self._attr_is_on = True
|
||||
self.async_write_ha_state()
|
||||
```
|
||||
|
||||
## Testing Switches
|
||||
|
||||
### Snapshot Testing
|
||||
|
||||
```python
|
||||
"""Test switches."""
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_switches(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
init_integration,
|
||||
) -> None:
|
||||
"""Test switch entities."""
|
||||
await snapshot_platform(
|
||||
hass,
|
||||
entity_registry,
|
||||
snapshot,
|
||||
mock_config_entry.entry_id,
|
||||
)
|
||||
```
|
||||
|
||||
### Control Testing
|
||||
|
||||
```python
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
)
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
|
||||
|
||||
async def test_switch_on_off(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_device,
|
||||
) -> None:
|
||||
"""Test turning switch on and off."""
|
||||
await init_integration(hass, mock_config_entry)
|
||||
|
||||
# Test initial state
|
||||
state = hass.states.get("switch.my_device_outlet")
|
||||
assert state
|
||||
assert state.state == "off"
|
||||
|
||||
# Turn on
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: "switch.my_device_outlet"},
|
||||
blocking=True,
|
||||
)
|
||||
mock_device.turn_on.assert_called_once()
|
||||
|
||||
# Check state updated
|
||||
state = hass.states.get("switch.my_device_outlet")
|
||||
assert state.state == "on"
|
||||
|
||||
# Turn off
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
{ATTR_ENTITY_ID: "switch.my_device_outlet"},
|
||||
blocking=True,
|
||||
)
|
||||
mock_device.turn_off.assert_called_once()
|
||||
|
||||
state = hass.states.get("switch.my_device_outlet")
|
||||
assert state.state == "off"
|
||||
```
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Pattern 1: Coordinator-Based Switch
|
||||
|
||||
```python
|
||||
class MySwitch(CoordinatorEntity[MyCoordinator], SwitchEntity):
|
||||
"""Coordinator-based switch."""
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn on."""
|
||||
await self.coordinator.client.turn_on(self.device_id)
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn off."""
|
||||
await self.coordinator.client.turn_off(self.device_id)
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return if switch is on."""
|
||||
if device := self.coordinator.data.devices.get(self.device_id):
|
||||
return device.is_on
|
||||
return None
|
||||
```
|
||||
|
||||
### Pattern 2: Local State Management
|
||||
|
||||
```python
|
||||
class MyLocalSwitch(SwitchEntity):
|
||||
"""Switch with local state."""
|
||||
|
||||
_attr_should_poll = False
|
||||
_attr_is_on = False
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn on."""
|
||||
await self.device.turn_on()
|
||||
self._attr_is_on = True
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn off."""
|
||||
await self.device.turn_off()
|
||||
self._attr_is_on = False
|
||||
self.async_write_ha_state()
|
||||
```
|
||||
|
||||
### Pattern 3: With Additional Control
|
||||
|
||||
```python
|
||||
class MyAdvancedSwitch(SwitchEntity):
|
||||
"""Switch with timer support."""
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn on with optional duration."""
|
||||
duration = kwargs.get("duration") # Custom kwarg
|
||||
|
||||
if duration:
|
||||
await self.device.turn_on_for(duration)
|
||||
else:
|
||||
await self.device.turn_on()
|
||||
|
||||
await self.coordinator.async_request_refresh()
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### ✅ DO
|
||||
|
||||
- Implement both turn_on and turn_off
|
||||
- Update state after commands
|
||||
- Handle errors properly
|
||||
- Use coordinator for state management
|
||||
- Implement unique IDs
|
||||
- Use translation keys
|
||||
- Mark config switches with entity_category
|
||||
- Refresh coordinator after commands
|
||||
|
||||
### ❌ DON'T
|
||||
|
||||
- Block the event loop
|
||||
- Ignore errors silently
|
||||
- Create switches without unique IDs
|
||||
- Mix control and sensing (use separate entities)
|
||||
- Poll unnecessarily
|
||||
- Hardcode entity names
|
||||
- Forget to update state after commands
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Switch Not Responding
|
||||
|
||||
Check:
|
||||
- [ ] turn_on/turn_off methods are async
|
||||
- [ ] Not blocking the event loop
|
||||
- [ ] API client is working
|
||||
- [ ] Errors are being raised properly
|
||||
|
||||
### State Not Updating
|
||||
|
||||
Check:
|
||||
- [ ] async_write_ha_state() is called
|
||||
- [ ] Coordinator refresh is working
|
||||
- [ ] is_on returns correct value
|
||||
- [ ] Push updates are subscribed
|
||||
|
||||
### Switch Appearing as Unavailable
|
||||
|
||||
Check:
|
||||
- [ ] Device connection is working
|
||||
- [ ] Coordinator update is successful
|
||||
- [ ] available property returns True
|
||||
- [ ] Entity is in coordinator.data
|
||||
|
||||
## Quality Scale Considerations
|
||||
|
||||
- **Bronze**: Unique ID required
|
||||
- **Gold**: Entity translations, device class (if applicable)
|
||||
- **Platinum**: Full type hints
|
||||
|
||||
## References
|
||||
|
||||
- [Switch Documentation](https://developers.home-assistant.io/docs/core/entity/switch)
|
||||
- [Switch Integration](https://www.home-assistant.io/integrations/switch/)
|
||||
@@ -1,285 +0,0 @@
|
||||
---
|
||||
name: code-review
|
||||
description: Review Home Assistant integration code for quality, best practices, and standards compliance. Use when reviewing pull requests, identifying anti-patterns, checking security vulnerabilities (OWASP), verifying async patterns, ensuring quality scale compliance, or providing comprehensive code feedback.
|
||||
---
|
||||
|
||||
# Code Review Skill for Home Assistant Integrations
|
||||
|
||||
You are an expert Home Assistant code reviewer with deep knowledge of Python, async programming, Home Assistant architecture, and integration best practices.
|
||||
|
||||
## Review Guidelines
|
||||
|
||||
### What to Review
|
||||
✅ **DO review and comment on:**
|
||||
- Architecture and design patterns
|
||||
- Async programming correctness
|
||||
- Error handling and edge cases
|
||||
- Security vulnerabilities (XSS, SQL injection, command injection, etc.)
|
||||
- Performance issues (blocking operations, inefficient loops)
|
||||
- Code organization and clarity
|
||||
- Compliance with Home Assistant patterns
|
||||
- Quality scale requirements
|
||||
- Missing functionality or incomplete implementations
|
||||
|
||||
❌ **DO NOT comment on:**
|
||||
- Missing imports (static analysis catches this)
|
||||
- Code formatting (Ruff handles this)
|
||||
- Minor style issues that linters catch
|
||||
|
||||
### Git Practices During Review
|
||||
⚠️ **CRITICAL**: After review has started:
|
||||
- **DO NOT amend commits**
|
||||
- **DO NOT squash commits**
|
||||
- **DO NOT rebase commits**
|
||||
- Reviewers need to see what changed since their last review
|
||||
|
||||
## Key Review Areas
|
||||
|
||||
### 1. Async Programming Patterns
|
||||
|
||||
#### ✅ Good Async Patterns
|
||||
```python
|
||||
# Proper async I/O
|
||||
data = await client.get_data()
|
||||
|
||||
# Using asyncio.sleep instead of time.sleep
|
||||
await asyncio.sleep(5)
|
||||
|
||||
# Executor for blocking operations
|
||||
result = await hass.async_add_executor_job(blocking_function, args)
|
||||
|
||||
# Gathering async operations
|
||||
results = await asyncio.gather(
|
||||
client.get_temp(),
|
||||
client.get_humidity(),
|
||||
)
|
||||
|
||||
# @callback for event loop safe functions
|
||||
@callback
|
||||
def async_update_callback(self, event):
|
||||
self.async_write_ha_state()
|
||||
```
|
||||
|
||||
#### ❌ Bad Async Patterns
|
||||
```python
|
||||
# Blocking operations in event loop
|
||||
data = requests.get(url) # ❌ Blocks event loop
|
||||
time.sleep(5) # ❌ Blocks event loop
|
||||
|
||||
# Awaiting in loops (use gather instead)
|
||||
for device in devices:
|
||||
data = await device.get_data() # ❌ Sequential, slow
|
||||
|
||||
# Reusing BleakClient instances
|
||||
await self.client.connect() # ❌ Don't reuse BleakClient
|
||||
```
|
||||
|
||||
### 2. Error Handling
|
||||
|
||||
#### ✅ Good Error Handling
|
||||
```python
|
||||
# Minimal try blocks, process outside
|
||||
try:
|
||||
data = await device.get_data()
|
||||
except DeviceError as err:
|
||||
_LOGGER.error("Failed to get data: %s", err)
|
||||
return
|
||||
|
||||
# Process data outside try block
|
||||
processed = data.get("value", 0) * 100
|
||||
self._attr_native_value = processed
|
||||
|
||||
# Proper exception types
|
||||
try:
|
||||
await client.connect()
|
||||
except asyncio.TimeoutError as ex:
|
||||
raise ConfigEntryNotReady(f"Timeout connecting to {host}") from ex
|
||||
except AuthError as ex:
|
||||
raise ConfigEntryAuthFailed("Invalid credentials") from ex
|
||||
```
|
||||
|
||||
#### ❌ Bad Error Handling
|
||||
```python
|
||||
# Too much code in try block
|
||||
try:
|
||||
data = await device.get_data()
|
||||
processed = data.get("value", 0) * 100 # ❌ Should be outside
|
||||
self._attr_native_value = processed
|
||||
except DeviceError:
|
||||
_LOGGER.error("Failed")
|
||||
|
||||
# Bare exceptions in regular code
|
||||
try:
|
||||
data = await device.get_data()
|
||||
except Exception: # ❌ Too broad (unless in config flow/background task)
|
||||
_LOGGER.error("Failed")
|
||||
|
||||
# Wrong exception type
|
||||
if end_date < start_date:
|
||||
raise ValueError("Invalid dates") # ❌ Should be ServiceValidationError
|
||||
```
|
||||
|
||||
### 3. Security Vulnerabilities
|
||||
|
||||
Check for OWASP Top 10 vulnerabilities:
|
||||
|
||||
```python
|
||||
# ❌ Command Injection
|
||||
os.system(f"ping {user_input}") # DANGEROUS
|
||||
|
||||
# ✅ Safe alternative
|
||||
await hass.async_add_executor_job(
|
||||
subprocess.run,
|
||||
["ping", user_input],
|
||||
check=True
|
||||
)
|
||||
|
||||
# ❌ Exposing secrets in diagnostics
|
||||
return {"api_key": entry.data[CONF_API_KEY]} # DANGEROUS
|
||||
|
||||
# ✅ Safe alternative
|
||||
return async_redact_data(entry.data, {CONF_API_KEY, CONF_PASSWORD})
|
||||
```
|
||||
|
||||
### 4. Configuration Flow Patterns
|
||||
|
||||
#### ✅ Good Config Flow
|
||||
```python
|
||||
class MyConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
VERSION = 1
|
||||
MINOR_VERSION = 1
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
errors = {}
|
||||
|
||||
if user_input is not None:
|
||||
try:
|
||||
await self._test_connection(user_input)
|
||||
except ConnectionError:
|
||||
errors["base"] = "cannot_connect"
|
||||
except AuthError:
|
||||
errors["base"] = "invalid_auth"
|
||||
except Exception: # ✅ Allowed in config flow
|
||||
errors["base"] = "unknown"
|
||||
else:
|
||||
await self.async_set_unique_id(device_id)
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
return self.async_create_entry(
|
||||
title=device_name,
|
||||
data=user_input,
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=vol.Schema({
|
||||
vol.Required(CONF_HOST): str,
|
||||
vol.Required(CONF_API_KEY): str,
|
||||
}),
|
||||
errors=errors,
|
||||
)
|
||||
```
|
||||
|
||||
### 5. Entity Patterns
|
||||
|
||||
#### ✅ Good Entity Patterns
|
||||
```python
|
||||
class MySensor(CoordinatorEntity[MyCoordinator], SensorEntity):
|
||||
_attr_has_entity_name = True
|
||||
_attr_translation_key = "temperature"
|
||||
|
||||
def __init__(self, coordinator: MyCoordinator, device_id: str) -> None:
|
||||
super().__init__(coordinator)
|
||||
self._attr_unique_id = f"{device_id}_temperature"
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, device_id)},
|
||||
name=coordinator.data[device_id].name,
|
||||
)
|
||||
|
||||
@property
|
||||
def native_value(self) -> float | None:
|
||||
if device_data := self.coordinator.data.get(self.device_id):
|
||||
return device_data.temperature
|
||||
return None
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
return super().available and self.device_id in self.coordinator.data
|
||||
```
|
||||
|
||||
### 6. Quality Scale Compliance
|
||||
|
||||
Review manifest.json and quality_scale.yaml:
|
||||
|
||||
```json
|
||||
{
|
||||
"domain": "my_integration",
|
||||
"name": "My Integration",
|
||||
"codeowners": ["@me"],
|
||||
"config_flow": true,
|
||||
"integration_type": "device",
|
||||
"iot_class": "cloud_polling",
|
||||
"quality_scale": "silver"
|
||||
}
|
||||
```
|
||||
|
||||
Check:
|
||||
- [ ] All required Bronze rules implemented or exempted
|
||||
- [ ] Rules match declared quality scale tier
|
||||
- [ ] Valid exemption reasons provided
|
||||
- [ ] manifest.json has all required fields
|
||||
|
||||
## Performance Patterns
|
||||
|
||||
### ✅ Good Performance
|
||||
```python
|
||||
# Parallel API calls
|
||||
temp, humidity = await asyncio.gather(
|
||||
api.get_temperature(),
|
||||
api.get_humidity(),
|
||||
)
|
||||
|
||||
# Efficient coordinator usage
|
||||
PARALLEL_UPDATES = 0 # Unlimited for coordinator-based
|
||||
```
|
||||
|
||||
### ❌ Bad Performance
|
||||
```python
|
||||
# Sequential API calls
|
||||
temp = await api.get_temperature()
|
||||
humidity = await api.get_humidity() # ❌ Should use gather
|
||||
|
||||
# User-configurable scan intervals
|
||||
vol.Optional("scan_interval"): cv.positive_int # ❌ Not allowed
|
||||
```
|
||||
|
||||
## Review Process
|
||||
|
||||
When reviewing code:
|
||||
|
||||
1. **Architecture Review**: Does it follow HA patterns?
|
||||
2. **Code Quality**: Are async patterns correct? Is error handling comprehensive?
|
||||
3. **Standards Compliance**: Quality scale requirements met?
|
||||
4. **Performance & Efficiency**: No blocking operations? Efficient API usage?
|
||||
5. **User Experience**: Clear error messages? Proper translations?
|
||||
|
||||
## Providing Feedback
|
||||
|
||||
Structure feedback as:
|
||||
1. **Summary**: Overall assessment
|
||||
2. **Critical Issues**: Must fix before merge
|
||||
3. **Suggestions**: Nice-to-have improvements
|
||||
4. **Positive Notes**: What's done well
|
||||
|
||||
Be specific with file:line references and provide code examples.
|
||||
|
||||
## Reference Files
|
||||
|
||||
For detailed patterns and best practices, see:
|
||||
- `.claude/references/diagnostics.md` - Diagnostics implementation
|
||||
- `.claude/references/sensor.md` - Sensor platform
|
||||
- `.claude/references/binary_sensor.md` - Binary sensor platform
|
||||
- `.claude/references/switch.md` - Switch platform
|
||||
- `.claude/references/button.md` - Button platform
|
||||
- `.claude/references/number.md` - Number platform
|
||||
- `.claude/references/select.md` - Select platform
|
||||
@@ -1,297 +0,0 @@
|
||||
---
|
||||
name: quality-scale-architect
|
||||
description: Provide architectural guidance and quality scale oversight for Home Assistant integrations. Use when designing integration structure, selecting quality tiers (Bronze/Silver/Gold/Platinum), recommending architectural patterns (coordinator/push/hub), planning quality progression, or advising on integration organization.
|
||||
---
|
||||
|
||||
# Quality Scale Architect for Home Assistant Integrations
|
||||
|
||||
You are an expert Home Assistant integration architect specializing in quality scale systems, best practices, and architectural patterns.
|
||||
|
||||
## Quality Scale System
|
||||
|
||||
### Quality Scale Tiers
|
||||
|
||||
**Bronze** - Basic Requirements (Mandatory for all integrations with quality scale)
|
||||
- ✅ Config flow (UI configuration)
|
||||
- ✅ Entity unique IDs
|
||||
- ✅ Action setup (or exempt)
|
||||
- ✅ Appropriate setup retries
|
||||
- ✅ Reauthentication flow
|
||||
- ✅ Reconfigure flow
|
||||
- ✅ Test coverage
|
||||
|
||||
**Silver** - Enhanced Functionality
|
||||
- All Bronze requirements +
|
||||
- ✅ Entity unavailable tracking
|
||||
- ✅ Parallel updates configuration
|
||||
- ✅ Runtime data storage
|
||||
- ✅ Unique config entry titles
|
||||
|
||||
**Gold** - Advanced Features
|
||||
- All Silver requirements +
|
||||
- ✅ Device registry usage
|
||||
- ✅ Integration diagnostics
|
||||
- ✅ Device diagnostics
|
||||
- ✅ Entity category
|
||||
- ✅ Device class
|
||||
- ✅ Disabled by default (for noisy entities)
|
||||
- ✅ Entity translations
|
||||
- ✅ Exception translations
|
||||
- ✅ Icon translations
|
||||
|
||||
**Platinum** - Highest Quality Standards
|
||||
- All Gold requirements +
|
||||
- ✅ Strict typing (full type hints)
|
||||
- ✅ Async dependencies (no sync-blocking libs)
|
||||
- ✅ WebSession injection
|
||||
- ✅ config_entry parameter in coordinator
|
||||
|
||||
## Architectural Patterns
|
||||
|
||||
### Pattern 1: Coordinator-Based Architecture
|
||||
**Use when**: Polling multiple entities from the same API
|
||||
|
||||
```python
|
||||
class MyCoordinator(DataUpdateCoordinator[MyData]):
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
client: MyClient,
|
||||
config_entry: ConfigEntry,
|
||||
) -> None:
|
||||
super().__init__(
|
||||
hass,
|
||||
logger=LOGGER,
|
||||
name=DOMAIN,
|
||||
update_interval=timedelta(minutes=5),
|
||||
config_entry=config_entry, # ✅ Pass for Platinum
|
||||
)
|
||||
self.client = client
|
||||
|
||||
async def _async_update_data(self) -> MyData:
|
||||
try:
|
||||
return await self.client.fetch_data()
|
||||
except ApiError as err:
|
||||
raise UpdateFailed(f"Error: {err}") from err
|
||||
```
|
||||
|
||||
### Pattern 2: Push-Based Architecture
|
||||
**Use when**: Device pushes updates (webhooks, MQTT, WebSocket)
|
||||
|
||||
```python
|
||||
class MyEntity(SensorEntity):
|
||||
async def async_added_to_hass(self) -> None:
|
||||
self.async_on_remove(
|
||||
self.hub.subscribe_updates(self._handle_update)
|
||||
)
|
||||
|
||||
@callback
|
||||
def _handle_update(self, data: dict) -> None:
|
||||
self._attr_native_value = data["value"]
|
||||
self.async_write_ha_state()
|
||||
```
|
||||
|
||||
### Pattern 3: Hub with Discovery
|
||||
**Use when**: Hub device with multiple discoverable endpoints
|
||||
|
||||
```python
|
||||
@callback
|
||||
def _check_new_devices() -> None:
|
||||
"""Check for new devices."""
|
||||
current = set(coordinator.data.devices.keys())
|
||||
new = current - known_devices
|
||||
|
||||
if new:
|
||||
known_devices.update(new)
|
||||
async_dispatcher_send(hass, f"{DOMAIN}_new_device", new)
|
||||
|
||||
entry.async_on_unload(coordinator.async_add_listener(_check_new_devices))
|
||||
```
|
||||
|
||||
## Architectural Decision Guide
|
||||
|
||||
### Choosing Integration Type
|
||||
|
||||
**Device Integration** (`"integration_type": "device"`)
|
||||
- Physical or virtual devices
|
||||
- Example: Smart plugs, thermostats, cameras
|
||||
|
||||
**Hub Integration** (`"integration_type": "hub"`)
|
||||
- Central hub controlling multiple devices
|
||||
- Example: Philips Hue bridge, Z-Wave controller
|
||||
|
||||
**Service Integration** (`"integration_type": "service"`)
|
||||
- Cloud services, APIs
|
||||
- Example: Weather services, notification platforms
|
||||
|
||||
**Helper Integration** (`"integration_type": "helper"`)
|
||||
- Utility integrations
|
||||
- Example: Template, group, automation helpers
|
||||
|
||||
### Choosing IoT Class
|
||||
|
||||
```json
|
||||
{
|
||||
"iot_class": "cloud_polling", // API polling
|
||||
"iot_class": "cloud_push", // Cloud webhooks/MQTT
|
||||
"iot_class": "local_polling", // Local device polling
|
||||
"iot_class": "local_push", // Local device push
|
||||
"iot_class": "calculated" // No external data
|
||||
}
|
||||
```
|
||||
|
||||
## Quality Scale Progression Strategy
|
||||
|
||||
### Starting Bronze (Minimum Viable Integration)
|
||||
|
||||
**Essential Components**:
|
||||
```
|
||||
homeassistant/components/my_integration/
|
||||
├── __init__.py # async_setup_entry, async_unload_entry
|
||||
├── manifest.json # Required fields, quality_scale: "bronze"
|
||||
├── const.py # DOMAIN constant
|
||||
├── config_flow.py # UI configuration with reauth/reconfigure
|
||||
├── sensor.py # Platform with unique IDs
|
||||
├── strings.json # Translations
|
||||
└── quality_scale.yaml # Rule tracking
|
||||
|
||||
tests/components/my_integration/
|
||||
├── conftest.py # Test fixtures
|
||||
├── test_config_flow.py # 100% coverage
|
||||
└── test_sensor.py # Entity tests
|
||||
```
|
||||
|
||||
**Bronze Checklist**:
|
||||
- [ ] Config flow with UI setup
|
||||
- [ ] Reauthentication flow
|
||||
- [ ] Reconfigure flow
|
||||
- [ ] All entities have unique IDs
|
||||
- [ ] Proper setup error handling
|
||||
- [ ] >95% test coverage
|
||||
- [ ] 100% config flow coverage
|
||||
|
||||
### Progressing to Silver
|
||||
|
||||
**Add**:
|
||||
- Entity unavailability tracking
|
||||
- Runtime data storage (not hass.data)
|
||||
- Parallel updates configuration
|
||||
- Unique entry titles
|
||||
|
||||
```python
|
||||
# Store in runtime_data (Silver requirement)
|
||||
entry.runtime_data = coordinator
|
||||
|
||||
# Entity availability (Silver requirement)
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
return super().available and self.device_id in self.coordinator.data
|
||||
|
||||
# Parallel updates (Silver requirement)
|
||||
PARALLEL_UPDATES = 0 # For coordinator-based
|
||||
```
|
||||
|
||||
### Progressing to Gold
|
||||
|
||||
**Add**:
|
||||
- Device registry entries
|
||||
- Integration & device diagnostics
|
||||
- Entity categories, device classes
|
||||
- Entity translations
|
||||
- Exception translations
|
||||
- Icon translations
|
||||
|
||||
```python
|
||||
# Device info (Gold requirement)
|
||||
_attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, device.id)},
|
||||
name=device.name,
|
||||
manufacturer="Manufacturer",
|
||||
model="Model",
|
||||
)
|
||||
|
||||
# Diagnostics (Gold requirement)
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
entry: MyConfigEntry,
|
||||
) -> dict[str, Any]:
|
||||
return {
|
||||
"data": async_redact_data(entry.data, TO_REDACT),
|
||||
"runtime": entry.runtime_data.to_dict(),
|
||||
}
|
||||
```
|
||||
|
||||
### Progressing to Platinum
|
||||
|
||||
**Add**:
|
||||
- Comprehensive type hints (py.typed)
|
||||
- Async-only dependencies
|
||||
- WebSession injection support
|
||||
|
||||
```python
|
||||
# Type hints (Platinum requirement)
|
||||
type MyIntegrationConfigEntry = ConfigEntry[MyClient]
|
||||
|
||||
# WebSession injection (Platinum requirement)
|
||||
client = MyClient(
|
||||
host=entry.data[CONF_HOST],
|
||||
session=async_get_clientsession(hass),
|
||||
)
|
||||
|
||||
# Pass config_entry to coordinator (Platinum requirement)
|
||||
coordinator = MyCoordinator(hass, client, entry)
|
||||
```
|
||||
|
||||
## Common Architectural Questions
|
||||
|
||||
### Q: Should I use a coordinator?
|
||||
**Use coordinator when**:
|
||||
- Polling API for multiple entities
|
||||
- Want efficient data sharing
|
||||
- Need coordinated updates
|
||||
|
||||
**Don't use coordinator when**:
|
||||
- Push-based updates (use callbacks)
|
||||
- Single entity integration
|
||||
- Each entity has independent data source
|
||||
|
||||
### Q: Where should I store runtime data?
|
||||
```python
|
||||
# ✅ GOOD - Use runtime_data (Silver+)
|
||||
entry.runtime_data = coordinator
|
||||
|
||||
# ❌ BAD - Don't use hass.data
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
||||
```
|
||||
|
||||
### Q: When should I create devices vs. just entities?
|
||||
**Create devices when**:
|
||||
- Representing physical/virtual devices
|
||||
- Multiple entities belong to same device
|
||||
- Want grouped device management
|
||||
|
||||
**Just entities when**:
|
||||
- Service integration (no physical device)
|
||||
- Single entity integration
|
||||
- Calculated/helper entities
|
||||
|
||||
## Reference Files
|
||||
|
||||
For detailed implementation guidance, see:
|
||||
- `.claude/references/diagnostics.md` - Diagnostics implementation
|
||||
- `.claude/references/sensor.md` - Sensor platform
|
||||
- `.claude/references/binary_sensor.md` - Binary sensor platform
|
||||
- `.claude/references/switch.md` - Switch platform
|
||||
- `.claude/references/button.md` - Button platform
|
||||
- `.claude/references/number.md` - Number platform
|
||||
- `.claude/references/select.md` - Select platform
|
||||
|
||||
## Your Task
|
||||
|
||||
When providing architectural guidance:
|
||||
|
||||
1. **Understand Requirements**: What is the integration type? What data needs exposure? Polling or push? What quality tier?
|
||||
2. **Recommend Architecture**: Suggest appropriate patterns, identify required components, explain decisions
|
||||
3. **Quality Scale Guidance**: Recommend starting tier, identify applicable rules, suggest progression path
|
||||
4. **Implementation Plan**: Outline file structure, identify key components, suggest implementation order
|
||||
5. **Best Practices**: Performance considerations, maintainability tips, common pitfalls to avoid
|
||||
@@ -1,205 +0,0 @@
|
||||
---
|
||||
name: testing
|
||||
description: Write, run, and fix tests for Home Assistant integrations. Use when writing comprehensive test coverage (>95%), running pytest, fixing failing tests, updating snapshots, or following HA testing patterns. Specializes in modern fixture patterns, config flow testing (100% coverage), entity snapshot testing, and mocking external APIs.
|
||||
---
|
||||
|
||||
# Testing Skill for Home Assistant Integrations
|
||||
|
||||
You are an expert Home Assistant integration test engineer specializing in writing comprehensive, maintainable tests that follow Home Assistant conventions and best practices.
|
||||
|
||||
## Testing Standards
|
||||
|
||||
### Coverage Requirements
|
||||
- **Minimum Coverage**: 95% for all modules
|
||||
- **Config Flow**: 100% coverage required for all paths
|
||||
- **Location**: Tests go in `tests/components/{domain}/`
|
||||
|
||||
### Test File Organization
|
||||
```
|
||||
tests/components/my_integration/
|
||||
├── __init__.py
|
||||
├── conftest.py # Fixtures and test setup
|
||||
├── test_config_flow.py # Config flow tests (100% coverage)
|
||||
├── test_sensor.py # Sensor platform tests
|
||||
├── test_init.py # Integration setup tests
|
||||
└── snapshots/ # Generated snapshot files
|
||||
```
|
||||
|
||||
## Modern Fixture Setup Pattern
|
||||
|
||||
Always use this pattern for integration tests:
|
||||
|
||||
```python
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.const import Platform
|
||||
from pytest_homeassistant_custom_component.common import MockConfigEntry
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_entry() -> MockConfigEntry:
|
||||
"""Return the default mocked config entry."""
|
||||
return MockConfigEntry(
|
||||
title="My Integration",
|
||||
domain=DOMAIN,
|
||||
data={CONF_HOST: "127.0.0.1", CONF_API_KEY: "test_key"},
|
||||
unique_id="device_unique_id",
|
||||
)
|
||||
|
||||
@pytest.fixture
|
||||
def mock_device_api() -> Generator[MagicMock]:
|
||||
"""Return a mocked device API."""
|
||||
with patch("homeassistant.components.my_integration.MyDeviceAPI", autospec=True) as api_mock:
|
||||
api = api_mock.return_value
|
||||
api.get_data.return_value = MyDeviceData.from_json(
|
||||
load_fixture("device_data.json", DOMAIN)
|
||||
)
|
||||
yield api
|
||||
|
||||
@pytest.fixture
|
||||
def platforms() -> list[Platform]:
|
||||
"""Fixture to specify platforms to test."""
|
||||
return [Platform.SENSOR]
|
||||
|
||||
@pytest.fixture
|
||||
async def init_integration(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_device_api: MagicMock,
|
||||
platforms: list[Platform],
|
||||
) -> MockConfigEntry:
|
||||
"""Set up the integration for testing."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
with patch("homeassistant.components.my_integration.PLATFORMS", platforms):
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return mock_config_entry
|
||||
```
|
||||
|
||||
## Entity Testing with Snapshots
|
||||
|
||||
Use snapshot testing for entity verification:
|
||||
|
||||
```python
|
||||
from syrupy import SnapshotAssertion
|
||||
from homeassistant.helpers import entity_registry as er, device_registry as dr
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default", "init_integration")
|
||||
async def test_entities(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the sensor entities."""
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
# Verify entities are assigned to device
|
||||
device_entry = device_registry.async_get_device(
|
||||
identifiers={(DOMAIN, "device_unique_id")}
|
||||
)
|
||||
assert device_entry
|
||||
entity_entries = er.async_entries_for_config_entry(
|
||||
entity_registry, mock_config_entry.entry_id
|
||||
)
|
||||
for entity_entry in entity_entries:
|
||||
assert entity_entry.device_id == device_entry.id
|
||||
```
|
||||
|
||||
## Config Flow Testing (100% Coverage Required)
|
||||
|
||||
Test ALL paths in config flow:
|
||||
|
||||
```python
|
||||
async def test_user_flow_success(hass, mock_api):
|
||||
"""Test successful user flow."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=TEST_USER_INPUT
|
||||
)
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "My Device"
|
||||
assert result["data"] == TEST_USER_INPUT
|
||||
|
||||
async def test_flow_connection_error(hass, mock_api_error):
|
||||
"""Test connection error handling."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=TEST_USER_INPUT
|
||||
)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
async def test_flow_duplicate_entry(hass, mock_config_entry, mock_api):
|
||||
"""Test duplicate entry prevention."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=TEST_USER_INPUT
|
||||
)
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
```
|
||||
|
||||
## Running Tests
|
||||
|
||||
### Integration-Specific Tests (Recommended)
|
||||
```bash
|
||||
pytest ./tests/components/<integration_domain> \
|
||||
--cov=homeassistant.components.<integration_domain> \
|
||||
--cov-report term-missing \
|
||||
--durations-min=1 \
|
||||
--durations=0 \
|
||||
--numprocesses=auto
|
||||
```
|
||||
|
||||
### Quick Test of Changed Files
|
||||
```bash
|
||||
pytest --timeout=10 --picked
|
||||
```
|
||||
|
||||
### Update Test Snapshots
|
||||
```bash
|
||||
pytest ./tests/components/<integration_domain> --snapshot-update
|
||||
```
|
||||
|
||||
**⚠️ IMPORTANT**: After using `--snapshot-update`:
|
||||
1. Run tests again WITHOUT the flag to verify snapshots
|
||||
2. Review the snapshot changes carefully
|
||||
3. Don't commit snapshot updates without verification
|
||||
|
||||
## Critical Testing Rules
|
||||
|
||||
### NEVER Do These Things
|
||||
- ❌ Don't access `hass.data` directly in tests
|
||||
- ❌ Don't test entities in isolation without integration setup
|
||||
- ❌ Don't forget to mock external dependencies
|
||||
|
||||
### ALWAYS Do These Things
|
||||
- ✅ Use proper integration setup through fixtures
|
||||
- ✅ Mock all external APIs
|
||||
- ✅ Test through the integration's public interface
|
||||
- ✅ Use snapshot testing for entities
|
||||
- ✅ Achieve 100% config flow coverage
|
||||
- ✅ Achieve >95% overall coverage
|
||||
|
||||
## Reference Files
|
||||
|
||||
For detailed implementation guidance, see:
|
||||
- `.claude/references/sensor.md` - Sensor platform patterns
|
||||
- `.claude/references/binary_sensor.md` - Binary sensor patterns
|
||||
- `.claude/references/switch.md` - Switch platform patterns
|
||||
- `.claude/references/button.md` - Button platform patterns
|
||||
- `.claude/references/number.md` - Number platform patterns
|
||||
- `.claude/references/select.md` - Select platform patterns
|
||||
@@ -40,7 +40,8 @@
|
||||
"python.terminal.activateEnvInCurrentTerminal": true,
|
||||
"python.testing.pytestArgs": ["--no-cov"],
|
||||
"pylint.importStrategy": "fromEnvironment",
|
||||
"python.analysis.typeCheckingMode": "basic",
|
||||
// Pyright type checking is not compatible with mypy which Home Assistant uses for type checking
|
||||
"python.analysis.typeCheckingMode": "off",
|
||||
"editor.formatOnPaste": false,
|
||||
"editor.formatOnSave": true,
|
||||
"editor.formatOnType": true,
|
||||
|
||||
4
.vscode/settings.default.jsonc
vendored
4
.vscode/settings.default.jsonc
vendored
@@ -7,8 +7,8 @@
|
||||
"python.testing.pytestEnabled": false,
|
||||
// https://code.visualstudio.com/docs/python/linting#_general-settings
|
||||
"pylint.importStrategy": "fromEnvironment",
|
||||
// Pyright is too pedantic for Home Assistant
|
||||
"python.analysis.typeCheckingMode": "basic",
|
||||
// Pyright type checking is not compatible with mypy which Home Assistant uses for type checking
|
||||
"python.analysis.typeCheckingMode": "off",
|
||||
"[python]": {
|
||||
"editor.defaultFormatter": "charliermarsh.ruff",
|
||||
},
|
||||
|
||||
@@ -85,6 +85,22 @@ class AirzoneSystemEntity(AirzoneEntity):
|
||||
value = system[key]
|
||||
return value
|
||||
|
||||
async def _async_update_sys_params(self, params: dict[str, Any]) -> None:
|
||||
"""Send system parameters to API."""
|
||||
_params = {
|
||||
API_SYSTEM_ID: self.system_id,
|
||||
**params,
|
||||
}
|
||||
_LOGGER.debug("update_sys_params=%s", _params)
|
||||
try:
|
||||
await self.coordinator.airzone.set_sys_parameters(_params)
|
||||
except AirzoneError as error:
|
||||
raise HomeAssistantError(
|
||||
f"Failed to set system {self.entity_id}: {error}"
|
||||
) from error
|
||||
|
||||
self.coordinator.async_set_updated_data(self.coordinator.airzone.data())
|
||||
|
||||
|
||||
class AirzoneHotWaterEntity(AirzoneEntity):
|
||||
"""Define an Airzone Hot Water entity."""
|
||||
|
||||
@@ -20,6 +20,7 @@ from aioairzone.const import (
|
||||
AZD_MODES,
|
||||
AZD_Q_ADAPT,
|
||||
AZD_SLEEP,
|
||||
AZD_SYSTEMS,
|
||||
AZD_ZONES,
|
||||
)
|
||||
|
||||
@@ -30,7 +31,7 @@ from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .coordinator import AirzoneConfigEntry, AirzoneUpdateCoordinator
|
||||
from .entity import AirzoneEntity, AirzoneZoneEntity
|
||||
from .entity import AirzoneEntity, AirzoneSystemEntity, AirzoneZoneEntity
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
@@ -85,14 +86,7 @@ def main_zone_options(
|
||||
return [k for k, v in options.items() if v in modes]
|
||||
|
||||
|
||||
MAIN_ZONE_SELECT_TYPES: Final[tuple[AirzoneSelectDescription, ...]] = (
|
||||
AirzoneSelectDescription(
|
||||
api_param=API_MODE,
|
||||
key=AZD_MODE,
|
||||
options_dict=MODE_DICT,
|
||||
options_fn=main_zone_options,
|
||||
translation_key="modes",
|
||||
),
|
||||
SYSTEM_SELECT_TYPES: Final[tuple[AirzoneSelectDescription, ...]] = (
|
||||
AirzoneSelectDescription(
|
||||
api_param=API_Q_ADAPT,
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
@@ -104,6 +98,17 @@ MAIN_ZONE_SELECT_TYPES: Final[tuple[AirzoneSelectDescription, ...]] = (
|
||||
)
|
||||
|
||||
|
||||
MAIN_ZONE_SELECT_TYPES: Final[tuple[AirzoneSelectDescription, ...]] = (
|
||||
AirzoneSelectDescription(
|
||||
api_param=API_MODE,
|
||||
key=AZD_MODE,
|
||||
options_dict=MODE_DICT,
|
||||
options_fn=main_zone_options,
|
||||
translation_key="modes",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
ZONE_SELECT_TYPES: Final[tuple[AirzoneSelectDescription, ...]] = (
|
||||
AirzoneSelectDescription(
|
||||
api_param=API_COLD_ANGLE,
|
||||
@@ -140,16 +145,37 @@ async def async_setup_entry(
|
||||
"""Add Airzone select from a config_entry."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
added_systems: set[str] = set()
|
||||
added_zones: set[str] = set()
|
||||
|
||||
def _async_entity_listener() -> None:
|
||||
"""Handle additions of select."""
|
||||
|
||||
entities: list[AirzoneBaseSelect] = []
|
||||
|
||||
systems_data = coordinator.data.get(AZD_SYSTEMS, {})
|
||||
received_systems = set(systems_data)
|
||||
new_systems = received_systems - added_systems
|
||||
if new_systems:
|
||||
entities.extend(
|
||||
AirzoneSystemSelect(
|
||||
coordinator,
|
||||
description,
|
||||
entry,
|
||||
system_id,
|
||||
systems_data.get(system_id),
|
||||
)
|
||||
for system_id in new_systems
|
||||
for description in SYSTEM_SELECT_TYPES
|
||||
if description.key in systems_data.get(system_id)
|
||||
)
|
||||
added_systems.update(new_systems)
|
||||
|
||||
zones_data = coordinator.data.get(AZD_ZONES, {})
|
||||
received_zones = set(zones_data)
|
||||
new_zones = received_zones - added_zones
|
||||
if new_zones:
|
||||
entities: list[AirzoneZoneSelect] = [
|
||||
entities.extend(
|
||||
AirzoneZoneSelect(
|
||||
coordinator,
|
||||
description,
|
||||
@@ -161,8 +187,8 @@ async def async_setup_entry(
|
||||
for description in MAIN_ZONE_SELECT_TYPES
|
||||
if description.key in zones_data.get(system_zone_id)
|
||||
and zones_data.get(system_zone_id).get(AZD_MASTER) is True
|
||||
]
|
||||
entities += [
|
||||
)
|
||||
entities.extend(
|
||||
AirzoneZoneSelect(
|
||||
coordinator,
|
||||
description,
|
||||
@@ -173,10 +199,11 @@ async def async_setup_entry(
|
||||
for system_zone_id in new_zones
|
||||
for description in ZONE_SELECT_TYPES
|
||||
if description.key in zones_data.get(system_zone_id)
|
||||
]
|
||||
async_add_entities(entities)
|
||||
)
|
||||
added_zones.update(new_zones)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
entry.async_on_unload(coordinator.async_add_listener(_async_entity_listener))
|
||||
_async_entity_listener()
|
||||
|
||||
@@ -203,6 +230,38 @@ class AirzoneBaseSelect(AirzoneEntity, SelectEntity):
|
||||
self._attr_current_option = self._get_current_option()
|
||||
|
||||
|
||||
class AirzoneSystemSelect(AirzoneSystemEntity, AirzoneBaseSelect):
|
||||
"""Define an Airzone System select."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: AirzoneUpdateCoordinator,
|
||||
description: AirzoneSelectDescription,
|
||||
entry: ConfigEntry,
|
||||
system_id: str,
|
||||
system_data: dict[str, Any],
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinator, entry, system_data)
|
||||
|
||||
self._attr_unique_id = f"{self._attr_unique_id}_{system_id}_{description.key}"
|
||||
self.entity_description = description
|
||||
|
||||
self._attr_options = self.entity_description.options_fn(
|
||||
system_data, description.options_dict
|
||||
)
|
||||
|
||||
self.values_dict = {v: k for k, v in description.options_dict.items()}
|
||||
|
||||
self._async_update_attrs()
|
||||
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Change the selected option."""
|
||||
param = self.entity_description.api_param
|
||||
value = self.entity_description.options_dict[option]
|
||||
await self._async_update_sys_params({param: value})
|
||||
|
||||
|
||||
class AirzoneZoneSelect(AirzoneZoneEntity, AirzoneBaseSelect):
|
||||
"""Define an Airzone Zone select."""
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
import dateutil
|
||||
|
||||
from homeassistant.components.automation import automations_with_entity
|
||||
from homeassistant.components.script import scripts_with_entity
|
||||
from homeassistant.components.sensor import (
|
||||
@@ -179,6 +181,7 @@ SENSORS: dict[str, SensorEntityDescription] = {
|
||||
LAST_S_TEST: SensorEntityDescription(
|
||||
key=LAST_S_TEST,
|
||||
translation_key="last_self_test",
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
),
|
||||
"lastxfer": SensorEntityDescription(
|
||||
key="lastxfer",
|
||||
@@ -232,6 +235,7 @@ SENSORS: dict[str, SensorEntityDescription] = {
|
||||
"masterupd": SensorEntityDescription(
|
||||
key="masterupd",
|
||||
translation_key="master_update",
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
"maxlinev": SensorEntityDescription(
|
||||
@@ -365,6 +369,7 @@ SENSORS: dict[str, SensorEntityDescription] = {
|
||||
"starttime": SensorEntityDescription(
|
||||
key="starttime",
|
||||
translation_key="startup_time",
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
"statflag": SensorEntityDescription(
|
||||
@@ -416,16 +421,19 @@ SENSORS: dict[str, SensorEntityDescription] = {
|
||||
"xoffbat": SensorEntityDescription(
|
||||
key="xoffbat",
|
||||
translation_key="transfer_from_battery",
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
"xoffbatt": SensorEntityDescription(
|
||||
key="xoffbatt",
|
||||
translation_key="transfer_from_battery",
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
"xonbatt": SensorEntityDescription(
|
||||
key="xonbatt",
|
||||
translation_key="transfer_to_battery",
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
}
|
||||
@@ -529,7 +537,13 @@ class APCUPSdSensor(APCUPSdEntity, SensorEntity):
|
||||
self._attr_native_value = None
|
||||
return
|
||||
|
||||
self._attr_native_value, inferred_unit = infer_unit(self.coordinator.data[key])
|
||||
data = self.coordinator.data[key]
|
||||
|
||||
if self.entity_description.device_class == SensorDeviceClass.TIMESTAMP:
|
||||
self._attr_native_value = dateutil.parser.parse(data)
|
||||
return
|
||||
|
||||
self._attr_native_value, inferred_unit = infer_unit(data)
|
||||
if not self.native_unit_of_measurement:
|
||||
self._attr_native_unit_of_measurement = inferred_unit
|
||||
|
||||
|
||||
@@ -111,11 +111,17 @@ class BSBLANClimate(BSBLanEntity, ClimateEntity):
|
||||
return None
|
||||
return self.coordinator.data.state.target_temperature.value
|
||||
|
||||
@property
|
||||
def _hvac_mode_value(self) -> int | str | None:
|
||||
"""Return the raw hvac_mode value from the coordinator."""
|
||||
if (hvac_mode := self.coordinator.data.state.hvac_mode) is None:
|
||||
return None
|
||||
return hvac_mode.value
|
||||
|
||||
@property
|
||||
def hvac_mode(self) -> HVACMode | None:
|
||||
"""Return hvac operation ie. heat, cool mode."""
|
||||
hvac_mode_value = self.coordinator.data.state.hvac_mode.value
|
||||
if hvac_mode_value is None:
|
||||
if (hvac_mode_value := self._hvac_mode_value) is None:
|
||||
return None
|
||||
# BSB-Lan returns integer values: 0=off, 1=auto, 2=eco, 3=heat
|
||||
if isinstance(hvac_mode_value, int):
|
||||
@@ -125,9 +131,8 @@ class BSBLANClimate(BSBLanEntity, ClimateEntity):
|
||||
@property
|
||||
def preset_mode(self) -> str | None:
|
||||
"""Return the current preset mode."""
|
||||
hvac_mode_value = self.coordinator.data.state.hvac_mode.value
|
||||
# BSB-Lan mode 2 is eco/reduced mode
|
||||
if hvac_mode_value == 2:
|
||||
if self._hvac_mode_value == 2:
|
||||
return PRESET_ECO
|
||||
return PRESET_NONE
|
||||
|
||||
|
||||
@@ -29,7 +29,11 @@ class BSBLanEntityBase[_T: BSBLanCoordinator](CoordinatorEntity[_T]):
|
||||
connections={(CONNECTION_NETWORK_MAC, format_mac(mac))},
|
||||
name=data.device.name,
|
||||
manufacturer="BSBLAN Inc.",
|
||||
model=data.info.device_identification.value,
|
||||
model=(
|
||||
data.info.device_identification.value
|
||||
if data.info.device_identification
|
||||
else None
|
||||
),
|
||||
sw_version=data.device.version,
|
||||
configuration_url=f"http://{host}",
|
||||
)
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
"integration_type": "device",
|
||||
"iot_class": "local_polling",
|
||||
"loggers": ["bsblan"],
|
||||
"requirements": ["python-bsblan==3.1.6"],
|
||||
"requirements": ["python-bsblan==4.1.0"],
|
||||
"zeroconf": [
|
||||
{
|
||||
"name": "bsb-lan*",
|
||||
|
||||
@@ -50,7 +50,6 @@ from . import (
|
||||
from .client import CloudClient
|
||||
from .const import (
|
||||
CONF_ACCOUNT_LINK_SERVER,
|
||||
CONF_ACCOUNTS_SERVER,
|
||||
CONF_ACME_SERVER,
|
||||
CONF_ALEXA,
|
||||
CONF_ALIASES,
|
||||
@@ -138,7 +137,6 @@ _BASE_CONFIG_SCHEMA = vol.Schema(
|
||||
vol.Optional(CONF_ALEXA): ALEXA_SCHEMA,
|
||||
vol.Optional(CONF_GOOGLE_ACTIONS): GACTIONS_SCHEMA,
|
||||
vol.Optional(CONF_ACCOUNT_LINK_SERVER): str,
|
||||
vol.Optional(CONF_ACCOUNTS_SERVER): str,
|
||||
vol.Optional(CONF_ACME_SERVER): str,
|
||||
vol.Optional(CONF_API_SERVER): str,
|
||||
vol.Optional(CONF_RELAYER_SERVER): str,
|
||||
|
||||
@@ -76,7 +76,6 @@ CONF_GOOGLE_ACTIONS = "google_actions"
|
||||
CONF_USER_POOL_ID = "user_pool_id"
|
||||
|
||||
CONF_ACCOUNT_LINK_SERVER = "account_link_server"
|
||||
CONF_ACCOUNTS_SERVER = "accounts_server"
|
||||
CONF_ACME_SERVER = "acme_server"
|
||||
CONF_API_SERVER = "api_server"
|
||||
CONF_DISCOVERY_SERVICE_ACTIONS = "discovery_service_actions"
|
||||
|
||||
@@ -13,6 +13,6 @@
|
||||
"integration_type": "system",
|
||||
"iot_class": "cloud_push",
|
||||
"loggers": ["acme", "hass_nabucasa", "snitun"],
|
||||
"requirements": ["hass-nabucasa==1.7.0"],
|
||||
"requirements": ["hass-nabucasa==1.9.0"],
|
||||
"single_config_entry": true
|
||||
}
|
||||
|
||||
@@ -461,7 +461,7 @@ FITBIT_RESOURCES_LIST: Final[tuple[FitbitSensorEntityDescription, ...]] = (
|
||||
key="sleep/timeInBed",
|
||||
translation_key="sleep_time_in_bed",
|
||||
native_unit_of_measurement=UnitOfTime.MINUTES,
|
||||
icon="mdi:hotel",
|
||||
icon="mdi:bed",
|
||||
device_class=SensorDeviceClass.DURATION,
|
||||
scope=FitbitScope.SLEEP,
|
||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||
|
||||
@@ -31,7 +31,7 @@ STEP_USER_DATA_SCHEMA = vol.Schema(
|
||||
)
|
||||
STEP_SMS_CODE_DATA_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_SMS_CODE): int,
|
||||
vol.Required(CONF_SMS_CODE): str,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -75,7 +75,7 @@ class FressnapfTrackerConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
return errors, False
|
||||
|
||||
async def _async_verify_sms_code(
|
||||
self, sms_code: int
|
||||
self, sms_code: str
|
||||
) -> tuple[dict[str, str], str | None]:
|
||||
"""Verify SMS code and return errors and access_token."""
|
||||
errors: dict[str, str] = {}
|
||||
|
||||
@@ -7,5 +7,5 @@
|
||||
"integration_type": "hub",
|
||||
"iot_class": "cloud_polling",
|
||||
"quality_scale": "bronze",
|
||||
"requirements": ["fressnapftracker==0.2.0"]
|
||||
"requirements": ["fressnapftracker==0.2.1"]
|
||||
}
|
||||
|
||||
@@ -164,13 +164,12 @@ def _async_wol_buttons_list(
|
||||
class FritzBoxWOLButton(FritzDeviceBase, ButtonEntity):
|
||||
"""Defines a FRITZ!Box Tools Wake On LAN button."""
|
||||
|
||||
_attr_icon = "mdi:lan-pending"
|
||||
_attr_entity_registry_enabled_default = False
|
||||
_attr_translation_key = "wake_on_lan"
|
||||
|
||||
def __init__(self, avm_wrapper: AvmWrapper, device: FritzDevice) -> None:
|
||||
"""Initialize Fritz!Box WOL button."""
|
||||
super().__init__(avm_wrapper, device)
|
||||
self._name = f"{self.hostname} Wake on LAN"
|
||||
self._attr_unique_id = f"{self._mac}_wake_on_lan"
|
||||
self._is_available = True
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import DEFAULT_DEVICE_NAME
|
||||
from .coordinator import FRITZ_DATA_KEY, AvmWrapper, FritzConfigEntry, FritzData
|
||||
from .entity import FritzDeviceBase
|
||||
from .helpers import device_filter_out_from_trackers
|
||||
@@ -71,6 +72,7 @@ class FritzBoxTracker(FritzDeviceBase, ScannerEntity):
|
||||
def __init__(self, avm_wrapper: AvmWrapper, device: FritzDevice) -> None:
|
||||
"""Initialize a FRITZ!Box device."""
|
||||
super().__init__(avm_wrapper, device)
|
||||
self._attr_name: str = device.hostname or DEFAULT_DEVICE_NAME
|
||||
self._last_activity: datetime.datetime | None = device.last_activity
|
||||
|
||||
@property
|
||||
|
||||
@@ -13,7 +13,7 @@ from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity import EntityDescription
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DEFAULT_DEVICE_NAME, DOMAIN
|
||||
from .const import DOMAIN
|
||||
from .coordinator import AvmWrapper
|
||||
from .models import FritzDevice
|
||||
|
||||
@@ -21,21 +21,17 @@ from .models import FritzDevice
|
||||
class FritzDeviceBase(CoordinatorEntity[AvmWrapper]):
|
||||
"""Entity base class for a device connected to a FRITZ!Box device."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(self, avm_wrapper: AvmWrapper, device: FritzDevice) -> None:
|
||||
"""Initialize a FRITZ!Box device."""
|
||||
super().__init__(avm_wrapper)
|
||||
self._avm_wrapper = avm_wrapper
|
||||
self._mac: str = device.mac_address
|
||||
self._name: str = device.hostname or DEFAULT_DEVICE_NAME
|
||||
self._attr_device_info = DeviceInfo(
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, device.mac_address)}
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return device name."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def ip_address(self) -> str | None:
|
||||
"""Return the primary ip address of the device."""
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
"button": {
|
||||
"cleanup": {
|
||||
"default": "mdi:broom"
|
||||
},
|
||||
"wake_on_lan": {
|
||||
"default": "mdi:lan-pending"
|
||||
}
|
||||
},
|
||||
"sensor": {
|
||||
@@ -48,6 +51,11 @@
|
||||
"max_kb_s_sent": {
|
||||
"default": "mdi:upload"
|
||||
}
|
||||
},
|
||||
"switch": {
|
||||
"internet_access": {
|
||||
"default": "mdi:router-wireless-settings"
|
||||
}
|
||||
}
|
||||
},
|
||||
"services": {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
"integration_type": "hub",
|
||||
"iot_class": "local_polling",
|
||||
"loggers": ["fritzconnection"],
|
||||
"quality_scale": "bronze",
|
||||
"requirements": ["fritzconnection[qr]==1.15.0", "xmltodict==1.0.2"],
|
||||
"ssdp": [
|
||||
{
|
||||
|
||||
@@ -13,9 +13,7 @@ rules:
|
||||
docs-removal-instructions: done
|
||||
entity-event-setup: done
|
||||
entity-unique-id: done
|
||||
has-entity-name:
|
||||
status: todo
|
||||
comment: partially done
|
||||
has-entity-name: done
|
||||
runtime-data: done
|
||||
test-before-configure: done
|
||||
test-before-setup: done
|
||||
|
||||
@@ -108,6 +108,9 @@
|
||||
},
|
||||
"reconnect": {
|
||||
"name": "Reconnect"
|
||||
},
|
||||
"wake_on_lan": {
|
||||
"name": "Wake on LAN"
|
||||
}
|
||||
},
|
||||
"sensor": {
|
||||
@@ -162,6 +165,11 @@
|
||||
"max_kb_s_sent": {
|
||||
"name": "Max connection upload throughput"
|
||||
}
|
||||
},
|
||||
"switch": {
|
||||
"internet_access": {
|
||||
"name": "Internet access"
|
||||
}
|
||||
}
|
||||
},
|
||||
"exceptions": {
|
||||
|
||||
@@ -499,13 +499,12 @@ class FritzBoxDeflectionSwitch(FritzBoxBaseCoordinatorSwitch):
|
||||
class FritzBoxProfileSwitch(FritzDeviceBase, SwitchEntity):
|
||||
"""Defines a FRITZ!Box Tools DeviceProfile switch."""
|
||||
|
||||
_attr_icon = "mdi:router-wireless-settings"
|
||||
_attr_translation_key = "internet_access"
|
||||
|
||||
def __init__(self, avm_wrapper: AvmWrapper, device: FritzDevice) -> None:
|
||||
"""Init Fritz profile."""
|
||||
super().__init__(avm_wrapper, device)
|
||||
self._attr_is_on: bool = False
|
||||
self._name = f"{device.hostname} Internet Access"
|
||||
self._attr_unique_id = f"{self._mac}_internet_access"
|
||||
self._attr_entity_category = EntityCategory.CONFIG
|
||||
|
||||
|
||||
@@ -23,5 +23,5 @@
|
||||
"winter_mode": {}
|
||||
},
|
||||
"quality_scale": "internal",
|
||||
"requirements": ["home-assistant-frontend==20260107.0"]
|
||||
"requirements": ["home-assistant-frontend==20260107.1"]
|
||||
}
|
||||
|
||||
@@ -1,9 +1,21 @@
|
||||
{
|
||||
"entity": {
|
||||
"sensor": {
|
||||
"ammonia": {
|
||||
"default": "mdi:molecule"
|
||||
},
|
||||
"benzene": {
|
||||
"default": "mdi:molecule"
|
||||
},
|
||||
"nitrogen_dioxide": {
|
||||
"default": "mdi:molecule"
|
||||
},
|
||||
"nitrogen_monoxide": {
|
||||
"default": "mdi:molecule"
|
||||
},
|
||||
"non_methane_hydrocarbons": {
|
||||
"default": "mdi:molecule"
|
||||
},
|
||||
"ozone": {
|
||||
"default": "mdi:molecule"
|
||||
},
|
||||
|
||||
@@ -99,6 +99,14 @@ AIR_QUALITY_SENSOR_TYPES: tuple[AirQualitySensorEntityDescription, ...] = (
|
||||
"local_aqi": data.indexes[1].display_name
|
||||
},
|
||||
),
|
||||
AirQualitySensorEntityDescription(
|
||||
key="c6h6",
|
||||
translation_key="benzene",
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
native_unit_of_measurement_fn=lambda x: x.pollutants.c6h6.concentration.units,
|
||||
value_fn=lambda x: x.pollutants.c6h6.concentration.value,
|
||||
exists_fn=lambda x: "c6h6" in {p.code for p in x.pollutants},
|
||||
),
|
||||
AirQualitySensorEntityDescription(
|
||||
key="co",
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
@@ -106,6 +114,30 @@ AIR_QUALITY_SENSOR_TYPES: tuple[AirQualitySensorEntityDescription, ...] = (
|
||||
native_unit_of_measurement_fn=lambda x: x.pollutants.co.concentration.units,
|
||||
value_fn=lambda x: x.pollutants.co.concentration.value,
|
||||
),
|
||||
AirQualitySensorEntityDescription(
|
||||
key="nh3",
|
||||
translation_key="ammonia",
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
native_unit_of_measurement_fn=lambda x: x.pollutants.nh3.concentration.units,
|
||||
value_fn=lambda x: x.pollutants.nh3.concentration.value,
|
||||
exists_fn=lambda x: "nh3" in {p.code for p in x.pollutants},
|
||||
),
|
||||
AirQualitySensorEntityDescription(
|
||||
key="nmhc",
|
||||
translation_key="non_methane_hydrocarbons",
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
native_unit_of_measurement_fn=lambda x: x.pollutants.nmhc.concentration.units,
|
||||
value_fn=lambda x: x.pollutants.nmhc.concentration.value,
|
||||
exists_fn=lambda x: "nmhc" in {p.code for p in x.pollutants},
|
||||
),
|
||||
AirQualitySensorEntityDescription(
|
||||
key="no",
|
||||
translation_key="nitrogen_monoxide",
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
native_unit_of_measurement_fn=lambda x: x.pollutants.no.concentration.units,
|
||||
value_fn=lambda x: x.pollutants.no.concentration.value,
|
||||
exists_fn=lambda x: "no" in {p.code for p in x.pollutants},
|
||||
),
|
||||
AirQualitySensorEntityDescription(
|
||||
key="no2",
|
||||
translation_key="nitrogen_dioxide",
|
||||
|
||||
@@ -76,6 +76,12 @@
|
||||
},
|
||||
"entity": {
|
||||
"sensor": {
|
||||
"ammonia": {
|
||||
"name": "Ammonia"
|
||||
},
|
||||
"benzene": {
|
||||
"name": "Benzene"
|
||||
},
|
||||
"local_aqi": {
|
||||
"name": "{local_aqi} AQI"
|
||||
},
|
||||
@@ -189,6 +195,9 @@
|
||||
"name": "{local_aqi} dominant pollutant",
|
||||
"state": {
|
||||
"co": "[%key:component::sensor::entity_component::carbon_monoxide::name%]",
|
||||
"nh3": "[%key:component::google_air_quality::entity::sensor::ammonia::name%]",
|
||||
"nmhc": "[%key:component::google_air_quality::entity::sensor::non_methane_hydrocarbons::name%]",
|
||||
"no": "[%key:component::sensor::entity_component::nitrogen_monoxide::name%]",
|
||||
"no2": "[%key:component::sensor::entity_component::nitrogen_dioxide::name%]",
|
||||
"o3": "[%key:component::sensor::entity_component::ozone::name%]",
|
||||
"pm10": "[%key:component::sensor::entity_component::pm10::name%]",
|
||||
@@ -199,6 +208,12 @@
|
||||
"nitrogen_dioxide": {
|
||||
"name": "[%key:component::sensor::entity_component::nitrogen_dioxide::name%]"
|
||||
},
|
||||
"nitrogen_monoxide": {
|
||||
"name": "[%key:component::sensor::entity_component::nitrogen_monoxide::name%]"
|
||||
},
|
||||
"non_methane_hydrocarbons": {
|
||||
"name": "Non-methane hydrocarbons"
|
||||
},
|
||||
"ozone": {
|
||||
"name": "[%key:component::sensor::entity_component::ozone::name%]"
|
||||
},
|
||||
|
||||
21
homeassistant/components/hdfury/diagnostics.py
Normal file
21
homeassistant/components/hdfury/diagnostics.py
Normal file
@@ -0,0 +1,21 @@
|
||||
"""Diagnostics for HDFury Integration."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .coordinator import HDFuryCoordinator
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
coordinator: HDFuryCoordinator = entry.runtime_data
|
||||
|
||||
return {
|
||||
"board": coordinator.data.board,
|
||||
"info": coordinator.data.info,
|
||||
"config": coordinator.data.config,
|
||||
}
|
||||
@@ -43,7 +43,7 @@ rules:
|
||||
|
||||
# Gold
|
||||
devices: done
|
||||
diagnostics: todo
|
||||
diagnostics: done
|
||||
discovery-update-info: todo
|
||||
discovery: todo
|
||||
docs-data-update: todo
|
||||
|
||||
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
import logging
|
||||
|
||||
from pyhik.constants import SENSOR_MAP
|
||||
from pyhik.hikvision import HikCamera
|
||||
import requests
|
||||
|
||||
@@ -70,13 +71,33 @@ async def async_setup_entry(hass: HomeAssistant, entry: HikvisionConfigEntry) ->
|
||||
device_type=device_type,
|
||||
)
|
||||
|
||||
_LOGGER.debug(
|
||||
"Device %s (type=%s) initial event_states: %s",
|
||||
device_name,
|
||||
device_type,
|
||||
camera.current_event_states,
|
||||
)
|
||||
|
||||
# For NVRs or devices with no detected events, try to fetch events from ISAPI
|
||||
# Use broader notification methods for NVRs since they often use 'record' etc.
|
||||
if device_type == "NVR" or not camera.current_event_states:
|
||||
nvr_notification_methods = {"center", "HTTP", "record", "email", "beep"}
|
||||
|
||||
def fetch_and_inject_nvr_events() -> None:
|
||||
"""Fetch and inject NVR events in a single executor job."""
|
||||
if nvr_events := camera.get_event_triggers():
|
||||
camera.inject_events(nvr_events)
|
||||
nvr_events = camera.get_event_triggers(nvr_notification_methods)
|
||||
_LOGGER.debug("NVR events fetched with extended methods: %s", nvr_events)
|
||||
if nvr_events:
|
||||
# Map raw event type names to friendly names using SENSOR_MAP
|
||||
mapped_events: dict[str, list[int]] = {}
|
||||
for event_type, channels in nvr_events.items():
|
||||
friendly_name = SENSOR_MAP.get(event_type.lower(), event_type)
|
||||
if friendly_name in mapped_events:
|
||||
mapped_events[friendly_name].extend(channels)
|
||||
else:
|
||||
mapped_events[friendly_name] = list(channels)
|
||||
_LOGGER.debug("Mapped NVR events: %s", mapped_events)
|
||||
camera.inject_events(mapped_events)
|
||||
|
||||
await hass.async_add_executor_job(fetch_and_inject_nvr_events)
|
||||
|
||||
|
||||
@@ -13,6 +13,6 @@
|
||||
"iot_class": "local_polling",
|
||||
"loggers": ["homewizard_energy"],
|
||||
"quality_scale": "platinum",
|
||||
"requirements": ["python-homewizard-energy==10.0.0"],
|
||||
"requirements": ["python-homewizard-energy==10.0.1"],
|
||||
"zeroconf": ["_hwenergy._tcp.local.", "_homewizard._tcp.local."]
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
"loggers": ["xknx", "xknxproject"],
|
||||
"quality_scale": "platinum",
|
||||
"requirements": [
|
||||
"xknx==3.13.0",
|
||||
"xknx==3.14.0",
|
||||
"xknxproject==3.8.2",
|
||||
"knx-frontend==2025.12.30.151231"
|
||||
],
|
||||
|
||||
@@ -256,6 +256,8 @@ ENTITIES: tuple[LaMarzoccoNumberEntityDescription, ...] = (
|
||||
supported_fn=(
|
||||
lambda coordinator: coordinator.device.dashboard.model_name
|
||||
in (ModelName.LINEA_MINI, ModelName.LINEA_MINI_R)
|
||||
and WidgetType.CM_BREW_BY_WEIGHT_DOSES
|
||||
in coordinator.device.dashboard.config
|
||||
),
|
||||
),
|
||||
LaMarzoccoNumberEntityDescription(
|
||||
@@ -289,6 +291,8 @@ ENTITIES: tuple[LaMarzoccoNumberEntityDescription, ...] = (
|
||||
supported_fn=(
|
||||
lambda coordinator: coordinator.device.dashboard.model_name
|
||||
in (ModelName.LINEA_MINI, ModelName.LINEA_MINI_R)
|
||||
and WidgetType.CM_BREW_BY_WEIGHT_DOSES
|
||||
in coordinator.device.dashboard.config
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -149,6 +149,8 @@ ENTITIES: tuple[LaMarzoccoSelectEntityDescription, ...] = (
|
||||
supported_fn=(
|
||||
lambda coordinator: coordinator.device.dashboard.model_name
|
||||
in (ModelName.LINEA_MINI, ModelName.LINEA_MINI_R)
|
||||
and WidgetType.CM_BREW_BY_WEIGHT_DOSES
|
||||
in coordinator.device.dashboard.config
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -42,7 +42,7 @@
|
||||
},
|
||||
"conditions": {
|
||||
"is_off": {
|
||||
"description": "Test if a light is off.",
|
||||
"description": "Tests if one or more lights are off.",
|
||||
"fields": {
|
||||
"behavior": {
|
||||
"description": "[%key:component::light::common::condition_behavior_description%]",
|
||||
@@ -52,7 +52,7 @@
|
||||
"name": "If a light is off"
|
||||
},
|
||||
"is_on": {
|
||||
"description": "Test if a light is on.",
|
||||
"description": "Tests if one or more lights are on.",
|
||||
"fields": {
|
||||
"behavior": {
|
||||
"description": "[%key:component::light::common::condition_behavior_description%]",
|
||||
|
||||
@@ -66,8 +66,9 @@ class MatterRangeNumberEntityDescription(
|
||||
format_max_value: Callable[[float], float] = lambda x: x
|
||||
|
||||
# command: a custom callback to create the command to send to the device
|
||||
# the callback's argument will be the index of the selected list value
|
||||
command: Callable[[int], ClusterCommand]
|
||||
# the callback's argument will be the converted device value from ha_to_device
|
||||
# if omitted the command will just be a write_attribute command to the primary attribute
|
||||
command: Callable[[int], ClusterCommand] | None = None
|
||||
|
||||
|
||||
class MatterNumber(MatterEntity, NumberEntity):
|
||||
@@ -99,9 +100,15 @@ class MatterRangeNumber(MatterEntity, NumberEntity):
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Update the current value."""
|
||||
send_value = self.entity_description.ha_to_device(value)
|
||||
# custom command defined to set the new value
|
||||
await self.send_device_command(
|
||||
self.entity_description.command(send_value),
|
||||
if self.entity_description.command:
|
||||
# custom command defined to set the new value
|
||||
await self.send_device_command(
|
||||
self.entity_description.command(send_value),
|
||||
)
|
||||
return
|
||||
# regular write attribute to set the new value
|
||||
await self.write_attribute(
|
||||
value=send_value,
|
||||
)
|
||||
|
||||
@callback
|
||||
@@ -253,6 +260,30 @@ DISCOVERY_SCHEMAS = [
|
||||
entity_class=MatterNumber,
|
||||
required_attributes=(custom_clusters.EveCluster.Attributes.Altitude,),
|
||||
),
|
||||
MatterDiscoverySchema(
|
||||
platform=Platform.NUMBER,
|
||||
entity_description=MatterRangeNumberEntityDescription(
|
||||
key="ThermostatOccupiedSetback",
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
translation_key="occupied_setback",
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
device_to_ha=lambda x: None if x is None else x / 10,
|
||||
ha_to_device=lambda x: round(x * 10),
|
||||
format_min_value=lambda x: x / 10,
|
||||
format_max_value=lambda x: x / 10,
|
||||
min_attribute=clusters.Thermostat.Attributes.OccupiedSetbackMin,
|
||||
max_attribute=clusters.Thermostat.Attributes.OccupiedSetbackMax,
|
||||
native_step=0.5,
|
||||
mode=NumberMode.BOX,
|
||||
),
|
||||
entity_class=MatterRangeNumber,
|
||||
required_attributes=(
|
||||
clusters.Thermostat.Attributes.OccupiedSetback,
|
||||
clusters.Thermostat.Attributes.OccupiedSetbackMin,
|
||||
clusters.Thermostat.Attributes.OccupiedSetbackMax,
|
||||
),
|
||||
featuremap_contains=(clusters.Thermostat.Bitmaps.Feature.kSetback),
|
||||
),
|
||||
MatterDiscoverySchema(
|
||||
platform=Platform.NUMBER,
|
||||
entity_description=MatterNumberEntityDescription(
|
||||
|
||||
@@ -217,6 +217,9 @@
|
||||
"led_indicator_intensity_on": {
|
||||
"name": "LED on intensity"
|
||||
},
|
||||
"occupied_setback": {
|
||||
"name": "Occupied setback"
|
||||
},
|
||||
"off_transition_time": {
|
||||
"name": "Off transition time"
|
||||
},
|
||||
|
||||
@@ -7,6 +7,7 @@ from mill_local import OperationMode
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.climate import (
|
||||
ATTR_HVAC_MODE,
|
||||
ClimateEntity,
|
||||
ClimateEntityFeature,
|
||||
HVACAction,
|
||||
@@ -111,13 +112,16 @@ class MillHeater(MillBaseEntity, ClimateEntity):
|
||||
super().__init__(coordinator, device)
|
||||
|
||||
async def async_set_temperature(self, **kwargs: Any) -> None:
|
||||
"""Set new target temperature."""
|
||||
"""Set new target temperature and optionally HVAC mode."""
|
||||
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
|
||||
return
|
||||
await self.coordinator.mill_data_connection.set_heater_temp(
|
||||
self._id, float(temperature)
|
||||
)
|
||||
await self.coordinator.async_request_refresh()
|
||||
if (hvac_mode := kwargs.get(ATTR_HVAC_MODE)) is not None:
|
||||
await self.async_handle_set_hvac_mode_service(hvac_mode)
|
||||
else:
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
|
||||
"""Set new target hvac mode."""
|
||||
@@ -125,12 +129,11 @@ class MillHeater(MillBaseEntity, ClimateEntity):
|
||||
await self.coordinator.mill_data_connection.heater_control(
|
||||
self._id, power_status=True
|
||||
)
|
||||
await self.coordinator.async_request_refresh()
|
||||
elif hvac_mode == HVACMode.OFF:
|
||||
await self.coordinator.mill_data_connection.heater_control(
|
||||
self._id, power_status=False
|
||||
)
|
||||
await self.coordinator.async_request_refresh()
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
@callback
|
||||
def _update_attr(self, device: mill.Heater) -> None:
|
||||
@@ -189,25 +192,26 @@ class LocalMillHeater(CoordinatorEntity[MillDataUpdateCoordinator], ClimateEntit
|
||||
self._update_attr()
|
||||
|
||||
async def async_set_temperature(self, **kwargs: Any) -> None:
|
||||
"""Set new target temperature."""
|
||||
"""Set new target temperature and optionally HVAC mode."""
|
||||
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
|
||||
return
|
||||
await self.coordinator.mill_data_connection.set_target_temperature(
|
||||
float(temperature)
|
||||
)
|
||||
await self.coordinator.async_request_refresh()
|
||||
if (hvac_mode := kwargs.get(ATTR_HVAC_MODE)) is not None:
|
||||
await self.async_handle_set_hvac_mode_service(hvac_mode)
|
||||
else:
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
|
||||
"""Set new target hvac mode."""
|
||||
if hvac_mode == HVACMode.HEAT:
|
||||
await self.coordinator.mill_data_connection.set_operation_mode_control_individually()
|
||||
await self.coordinator.async_request_refresh()
|
||||
elif hvac_mode == HVACMode.OFF:
|
||||
await self.coordinator.mill_data_connection.set_operation_mode_off()
|
||||
await self.coordinator.async_request_refresh()
|
||||
elif hvac_mode == HVACMode.AUTO:
|
||||
await self.coordinator.mill_data_connection.set_operation_mode_weekly_program()
|
||||
await self.coordinator.async_request_refresh()
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
|
||||
@@ -47,7 +47,6 @@ rules:
|
||||
test-coverage:
|
||||
status: todo
|
||||
comment: |
|
||||
Use load_json_object_fixture in tests
|
||||
Patch the library instead of the HTTP requests
|
||||
Create a shared fixture for the mock config entry
|
||||
Use init_integration in tests
|
||||
|
||||
@@ -28,7 +28,7 @@ DEVICE_SUPPORT = {
|
||||
"3A": (),
|
||||
"3B": (),
|
||||
"42": (),
|
||||
"7E": ("EDS0066", "EDS0068"),
|
||||
"7E": ("EDS0065", "EDS0066", "EDS0068"),
|
||||
"A6": (),
|
||||
"EF": ("HB_HUB", "HB_MOISTURE_METER", "HobbyBoards_EF"),
|
||||
}
|
||||
|
||||
@@ -297,6 +297,20 @@ HOBBYBOARD_EF: dict[str, tuple[OneWireSensorEntityDescription, ...]] = {
|
||||
# 7E sensors are special sensors by Embedded Data Systems
|
||||
|
||||
EDS_SENSORS: dict[str, tuple[OneWireSensorEntityDescription, ...]] = {
|
||||
"EDS0065": (
|
||||
OneWireSensorEntityDescription(
|
||||
key="EDS0065/temperature",
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
OneWireSensorEntityDescription(
|
||||
key="EDS0065/humidity",
|
||||
device_class=SensorDeviceClass.HUMIDITY,
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
),
|
||||
),
|
||||
"EDS0066": (
|
||||
OneWireSensorEntityDescription(
|
||||
key="EDS0066/temperature",
|
||||
|
||||
@@ -9,5 +9,5 @@
|
||||
"iot_class": "cloud_polling",
|
||||
"loggers": ["opower"],
|
||||
"quality_scale": "bronze",
|
||||
"requirements": ["opower==0.16.1"]
|
||||
"requirements": ["opower==0.16.2"]
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ from aiohttp import ClientError, ClientResponseError, web
|
||||
from pypoint import PointSession
|
||||
|
||||
from homeassistant.components import webhook
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_WEBHOOK_ID, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
@@ -21,14 +20,12 @@ from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
|
||||
from . import api
|
||||
from .const import CONF_WEBHOOK_URL, DOMAIN, EVENT_RECEIVED, SIGNAL_WEBHOOK
|
||||
from .coordinator import PointDataUpdateCoordinator
|
||||
from .coordinator import PointConfigEntry, PointDataUpdateCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||
|
||||
type PointConfigEntry = ConfigEntry[PointDataUpdateCoordinator]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: PointConfigEntry) -> bool:
|
||||
"""Set up Minut Point from a config entry."""
|
||||
@@ -59,7 +56,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: PointConfigEntry) -> boo
|
||||
|
||||
point_session = PointSession(auth)
|
||||
|
||||
coordinator = PointDataUpdateCoordinator(hass, point_session)
|
||||
coordinator = PointDataUpdateCoordinator(hass, point_session, entry)
|
||||
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
|
||||
@@ -16,8 +16,8 @@ from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from . import PointConfigEntry
|
||||
from .const import DOMAIN, SIGNAL_WEBHOOK
|
||||
from .coordinator import PointConfigEntry
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -15,9 +15,8 @@ from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from . import PointConfigEntry
|
||||
from .const import SIGNAL_WEBHOOK
|
||||
from .coordinator import PointDataUpdateCoordinator
|
||||
from .coordinator import PointConfigEntry, PointDataUpdateCoordinator
|
||||
from .entity import MinutPointEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -7,6 +7,7 @@ from typing import Any
|
||||
|
||||
from pypoint import PointSession
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
from homeassistant.util.dt import parse_datetime
|
||||
@@ -15,17 +16,24 @@ from .const import DOMAIN, SCAN_INTERVAL
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
type PointConfigEntry = ConfigEntry[PointDataUpdateCoordinator]
|
||||
|
||||
|
||||
class PointDataUpdateCoordinator(DataUpdateCoordinator[dict[str, dict[str, Any]]]):
|
||||
"""Class to manage fetching Point data from the API."""
|
||||
|
||||
def __init__(self, hass: HomeAssistant, point: PointSession) -> None:
|
||||
config_entry: PointConfigEntry
|
||||
|
||||
def __init__(
|
||||
self, hass: HomeAssistant, point: PointSession, config_entry: PointConfigEntry
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(
|
||||
hass,
|
||||
_LOGGER,
|
||||
name=DOMAIN,
|
||||
update_interval=SCAN_INTERVAL,
|
||||
config_entry=config_entry,
|
||||
)
|
||||
self.point = point
|
||||
self.device_updates: dict[str, datetime] = {}
|
||||
|
||||
@@ -14,8 +14,7 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
from . import PointConfigEntry
|
||||
from .coordinator import PointDataUpdateCoordinator
|
||||
from .coordinator import PointConfigEntry, PointDataUpdateCoordinator
|
||||
from .entity import MinutPointEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -11,6 +11,6 @@
|
||||
"documentation": "https://www.home-assistant.io/integrations/pooldose",
|
||||
"integration_type": "device",
|
||||
"iot_class": "local_polling",
|
||||
"quality_scale": "silver",
|
||||
"requirements": ["python-pooldose==0.8.1"]
|
||||
"quality_scale": "gold",
|
||||
"requirements": ["python-pooldose==0.8.2"]
|
||||
}
|
||||
|
||||
@@ -45,12 +45,12 @@ rules:
|
||||
discovery-update-info: done
|
||||
discovery: done
|
||||
docs-data-update: done
|
||||
docs-examples: todo
|
||||
docs-examples: done
|
||||
docs-known-limitations: done
|
||||
docs-supported-devices: done
|
||||
docs-supported-functions: done
|
||||
docs-troubleshooting: done
|
||||
docs-use-cases: todo
|
||||
docs-use-cases: done
|
||||
dynamic-devices:
|
||||
status: exempt
|
||||
comment: This integration does not support dynamic device discovery, as each config entry represents a single PoolDose device with all available entities.
|
||||
|
||||
@@ -40,12 +40,13 @@ class PortainerSwitchEntityDescription(SwitchEntityDescription):
|
||||
async def perform_action(
|
||||
action: str, portainer: Portainer, endpoint_id: int, container_id: str
|
||||
) -> None:
|
||||
"""Stop a container."""
|
||||
"""Perform an action on a container."""
|
||||
try:
|
||||
if action == "start":
|
||||
await portainer.start_container(endpoint_id, container_id)
|
||||
elif action == "stop":
|
||||
await portainer.stop_container(endpoint_id, container_id)
|
||||
match action:
|
||||
case "start":
|
||||
await portainer.start_container(endpoint_id, container_id)
|
||||
case "stop":
|
||||
await portainer.stop_container(endpoint_id, container_id)
|
||||
except PortainerAuthenticationError as err:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
|
||||
@@ -142,6 +142,8 @@ async def async_setup_entry(
|
||||
class ScrapeSensor(CoordinatorEntity[ScrapeCoordinator], ManualTriggerSensorEntity):
|
||||
"""Representation of a web scrape sensor."""
|
||||
|
||||
_sensor_name: str | None = None
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
@@ -162,14 +164,26 @@ class ScrapeSensor(CoordinatorEntity[ScrapeCoordinator], ManualTriggerSensorEnti
|
||||
self._value_template = value_template
|
||||
self._attr_native_value = None
|
||||
if not yaml and (unique_id := trigger_entity_config.get(CONF_UNIQUE_ID)):
|
||||
self._attr_name = None
|
||||
self._sensor_name = None
|
||||
self._attr_has_entity_name = True
|
||||
self._attr_device_info = DeviceInfo(
|
||||
entry_type=DeviceEntryType.SERVICE,
|
||||
identifiers={(DOMAIN, unique_id)},
|
||||
manufacturer="Scrape",
|
||||
name=self.name,
|
||||
name=self._rendered[CONF_NAME],
|
||||
)
|
||||
else:
|
||||
self._sensor_name = self._rendered.get(CONF_NAME)
|
||||
|
||||
@property
|
||||
def name(self) -> str | None:
|
||||
"""Return the name of the sensor.
|
||||
|
||||
Override needed because TriggerBaseEntity.name always returns the
|
||||
rendered name, ignoring _attr_name. When has_entity_name is True,
|
||||
we need name to return None to use the device name instead.
|
||||
"""
|
||||
return self._sensor_name
|
||||
|
||||
def _extract_value(self) -> Any:
|
||||
"""Parse the html extraction in the executor."""
|
||||
|
||||
@@ -6,5 +6,6 @@
|
||||
"documentation": "https://www.home-assistant.io/integrations/tplink_omada",
|
||||
"integration_type": "hub",
|
||||
"iot_class": "local_polling",
|
||||
"quality_scale": "bronze",
|
||||
"requirements": ["tplink-omada-client==1.5.3"]
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
"iot_class": "local_push",
|
||||
"loggers": ["uiprotect", "unifi_discovery"],
|
||||
"quality_scale": "platinum",
|
||||
"requirements": ["uiprotect==8.0.0", "unifi-discovery==1.2.0"],
|
||||
"requirements": ["uiprotect==8.1.1", "unifi-discovery==1.2.0"],
|
||||
"ssdp": [
|
||||
{
|
||||
"manufacturer": "Ubiquiti Networks",
|
||||
|
||||
@@ -6,5 +6,5 @@
|
||||
"documentation": "https://www.home-assistant.io/integrations/vallox",
|
||||
"iot_class": "local_polling",
|
||||
"loggers": ["vallox_websocket_api"],
|
||||
"requirements": ["vallox-websocket-api==5.3.0"]
|
||||
"requirements": ["vallox-websocket-api==6.0.0"]
|
||||
}
|
||||
|
||||
@@ -212,6 +212,7 @@ SENSOR_TYPES: tuple[YoLinkSensorEntityDescription, ...] = (
|
||||
key="battery",
|
||||
device_class=SensorDeviceClass.BATTERY,
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
exists_fn=lambda device: device.device_type in BATTERY_POWER_SENSOR,
|
||||
should_update_entity=lambda value: value is not None,
|
||||
@@ -251,9 +252,11 @@ SENSOR_TYPES: tuple[YoLinkSensorEntityDescription, ...] = (
|
||||
# mcu temperature
|
||||
YoLinkSensorEntityDescription(
|
||||
key="devTemperature",
|
||||
translation_key="device_temperature",
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
exists_fn=lambda device: device.device_type in MCU_DEV_TEMPERATURE_SENSOR,
|
||||
should_update_entity=lambda value: value is not None,
|
||||
value=lambda device, data: data.get("devTemperature"),
|
||||
|
||||
@@ -67,6 +67,9 @@
|
||||
"current_power": {
|
||||
"name": "Current power"
|
||||
},
|
||||
"device_temperature": {
|
||||
"name": "Device temperature"
|
||||
},
|
||||
"power_consumption": {
|
||||
"name": "Power consumption"
|
||||
},
|
||||
|
||||
@@ -38,12 +38,7 @@ from homeassistant.setup import SetupPhases, async_start_setup
|
||||
from homeassistant.util.async_ import create_eager_task
|
||||
from homeassistant.util.hass_dict import HassKey
|
||||
|
||||
from . import (
|
||||
device_registry as dev_reg,
|
||||
entity_registry as ent_reg,
|
||||
service,
|
||||
translation,
|
||||
)
|
||||
from . import device_registry as dr, entity_registry as er, service, translation
|
||||
from .deprecation import deprecated_function
|
||||
from .entity_registry import EntityRegistry, RegistryEntryDisabler, RegistryEntryHider
|
||||
from .event import async_call_later
|
||||
@@ -624,7 +619,7 @@ class EntityPlatform:
|
||||
event loop and will finish faster if we run them concurrently.
|
||||
"""
|
||||
results: list[BaseException | None] | None = None
|
||||
entity_registry = ent_reg.async_get(self.hass)
|
||||
entity_registry = er.async_get(self.hass)
|
||||
try:
|
||||
async with self.hass.timeout.async_timeout(timeout, self.domain):
|
||||
results = await asyncio.gather(
|
||||
@@ -676,7 +671,7 @@ class EntityPlatform:
|
||||
to the event loop so we can await the coros directly without
|
||||
scheduling them as tasks.
|
||||
"""
|
||||
entity_registry = ent_reg.async_get(self.hass)
|
||||
entity_registry = er.async_get(self.hass)
|
||||
try:
|
||||
async with self.hass.timeout.async_timeout(timeout, self.domain):
|
||||
for entity in entities:
|
||||
@@ -852,16 +847,16 @@ class EntityPlatform:
|
||||
entity.add_to_platform_abort()
|
||||
return
|
||||
|
||||
device: dev_reg.DeviceEntry | None
|
||||
device: dr.DeviceEntry | None
|
||||
if self.config_entry:
|
||||
if device_info := entity.device_info:
|
||||
try:
|
||||
device = dev_reg.async_get(self.hass).async_get_or_create(
|
||||
device = dr.async_get(self.hass).async_get_or_create(
|
||||
config_entry_id=self.config_entry.entry_id,
|
||||
config_subentry_id=config_subentry_id,
|
||||
**device_info,
|
||||
)
|
||||
except dev_reg.DeviceInfoError as exc:
|
||||
except dr.DeviceInfoError as exc:
|
||||
self.logger.error(
|
||||
"%s: Not adding entity with invalid device info: %s",
|
||||
self.platform_name,
|
||||
@@ -869,6 +864,8 @@ class EntityPlatform:
|
||||
)
|
||||
entity.add_to_platform_abort()
|
||||
return
|
||||
|
||||
entity.device_entry = device
|
||||
else:
|
||||
device = entity.device_entry
|
||||
else:
|
||||
@@ -929,8 +926,6 @@ class EntityPlatform:
|
||||
)
|
||||
|
||||
entity.registry_entry = entry
|
||||
if device:
|
||||
entity.device_entry = device
|
||||
entity.entity_id = entry.entity_id
|
||||
|
||||
else: # entity.unique_id is None
|
||||
@@ -1236,7 +1231,7 @@ class EntityPlatform:
|
||||
|
||||
@callback
|
||||
def async_calculate_suggested_object_id(
|
||||
entity: Entity, device: dev_reg.DeviceEntry | None
|
||||
entity: Entity, device: dr.DeviceEntry | None
|
||||
) -> str | None:
|
||||
"""Calculate the suggested object ID for an entity."""
|
||||
calculated_object_id: str | None = None
|
||||
|
||||
@@ -36,10 +36,10 @@ fnv-hash-fast==1.6.0
|
||||
go2rtc-client==0.4.0
|
||||
ha-ffmpeg==3.2.2
|
||||
habluetooth==5.8.0
|
||||
hass-nabucasa==1.7.0
|
||||
hass-nabucasa==1.9.0
|
||||
hassil==3.5.0
|
||||
home-assistant-bluetooth==1.13.1
|
||||
home-assistant-frontend==20260107.0
|
||||
home-assistant-frontend==20260107.1
|
||||
home-assistant-intents==2026.1.6
|
||||
httpx==0.28.1
|
||||
ifaddr==0.2.0
|
||||
|
||||
@@ -9,8 +9,6 @@ import logging
|
||||
import os
|
||||
from typing import Any
|
||||
|
||||
from packaging.requirements import Requirement
|
||||
|
||||
from .core import HomeAssistant, callback
|
||||
from .exceptions import HomeAssistantError
|
||||
from .helpers import singleton
|
||||
@@ -260,8 +258,13 @@ class RequirementsManager:
|
||||
"""
|
||||
if DEPRECATED_PACKAGES or self.hass.config.skip_pip_packages:
|
||||
all_requirements = {
|
||||
requirement_string: Requirement(requirement_string)
|
||||
requirement_string: requirement_details
|
||||
for requirement_string in requirements
|
||||
if (
|
||||
requirement_details := pkg_util.parse_requirement_safe(
|
||||
requirement_string
|
||||
)
|
||||
)
|
||||
}
|
||||
if DEPRECATED_PACKAGES:
|
||||
for requirement_string, requirement_details in all_requirements.items():
|
||||
@@ -272,9 +275,12 @@ class RequirementsManager:
|
||||
"" if is_built_in else "custom ",
|
||||
name,
|
||||
f"has requirement '{requirement_string}' which {reason}",
|
||||
f"This will stop working in Home Assistant {breaks_in_ha_version}, please"
|
||||
if breaks_in_ha_version
|
||||
else "Please",
|
||||
(
|
||||
"This will stop working in Home Assistant "
|
||||
f"{breaks_in_ha_version}, please"
|
||||
if breaks_in_ha_version
|
||||
else "Please"
|
||||
),
|
||||
async_suggest_report_issue(
|
||||
self.hass, integration_domain=name
|
||||
),
|
||||
|
||||
@@ -44,6 +44,39 @@ def get_installed_versions(specifiers: set[str]) -> set[str]:
|
||||
return {specifier for specifier in specifiers if is_installed(specifier)}
|
||||
|
||||
|
||||
def parse_requirement_safe(requirement_str: str) -> Requirement | None:
|
||||
"""Parse a requirement string into a Requirement object.
|
||||
|
||||
expected input is a pip compatible package specifier (requirement string)
|
||||
e.g. "package==1.0.0" or "package>=1.0.0,<2.0.0" or "package@git+https://..."
|
||||
|
||||
For backward compatibility, it also accepts a URL with a fragment
|
||||
e.g. "git+https://github.com/pypa/pip#pip>=1"
|
||||
|
||||
Returns None on a badly-formed requirement string.
|
||||
"""
|
||||
try:
|
||||
return Requirement(requirement_str)
|
||||
except InvalidRequirement:
|
||||
if "#" not in requirement_str:
|
||||
_LOGGER.error("Invalid requirement '%s'", requirement_str)
|
||||
return None
|
||||
|
||||
# This is likely a URL with a fragment
|
||||
# example: git+https://github.com/pypa/pip#pip>=1
|
||||
|
||||
# fragment support was originally used to install zip files, and
|
||||
# we no longer do this in Home Assistant. However, custom
|
||||
# components started using it to install packages from git
|
||||
# urls which would make it would be a breaking change to
|
||||
# remove it.
|
||||
try:
|
||||
return Requirement(urlparse(requirement_str).fragment)
|
||||
except InvalidRequirement:
|
||||
_LOGGER.error("Invalid requirement '%s'", requirement_str)
|
||||
return None
|
||||
|
||||
|
||||
def is_installed(requirement_str: str) -> bool:
|
||||
"""Check if a package is installed and will be loaded when we import it.
|
||||
|
||||
@@ -56,26 +89,8 @@ def is_installed(requirement_str: str) -> bool:
|
||||
Returns True when the requirement is met.
|
||||
Returns False when the package is not installed or doesn't meet req.
|
||||
"""
|
||||
try:
|
||||
req = Requirement(requirement_str)
|
||||
except InvalidRequirement:
|
||||
if "#" not in requirement_str:
|
||||
_LOGGER.error("Invalid requirement '%s'", requirement_str)
|
||||
return False
|
||||
|
||||
# This is likely a URL with a fragment
|
||||
# example: git+https://github.com/pypa/pip#pip>=1
|
||||
|
||||
# fragment support was originally used to install zip files, and
|
||||
# we no longer do this in Home Assistant. However, custom
|
||||
# components started using it to install packages from git
|
||||
# urls which would make it would be a breaking change to
|
||||
# remove it.
|
||||
try:
|
||||
req = Requirement(urlparse(requirement_str).fragment)
|
||||
except InvalidRequirement:
|
||||
_LOGGER.error("Invalid requirement '%s'", requirement_str)
|
||||
return False
|
||||
if (req := parse_requirement_safe(requirement_str)) is None:
|
||||
return False
|
||||
|
||||
try:
|
||||
if (installed_version := version(req.name)) is None:
|
||||
|
||||
@@ -48,7 +48,7 @@ dependencies = [
|
||||
"fnv-hash-fast==1.6.0",
|
||||
# hass-nabucasa is imported by helpers which don't depend on the cloud
|
||||
# integration
|
||||
"hass-nabucasa==1.7.0",
|
||||
"hass-nabucasa==1.9.0",
|
||||
# When bumping httpx, please check the version pins of
|
||||
# httpcore, anyio, and h11 in gen_requirements_all
|
||||
"httpx==0.28.1",
|
||||
|
||||
2
requirements.txt
generated
2
requirements.txt
generated
@@ -24,7 +24,7 @@ cronsim==2.7
|
||||
cryptography==46.0.2
|
||||
fnv-hash-fast==1.6.0
|
||||
ha-ffmpeg==3.2.2
|
||||
hass-nabucasa==1.7.0
|
||||
hass-nabucasa==1.9.0
|
||||
hassil==3.5.0
|
||||
home-assistant-bluetooth==1.13.1
|
||||
home-assistant-intents==2026.1.6
|
||||
|
||||
20
requirements_all.txt
generated
20
requirements_all.txt
generated
@@ -1011,7 +1011,7 @@ freebox-api==1.2.2
|
||||
freesms==0.2.0
|
||||
|
||||
# homeassistant.components.fressnapf_tracker
|
||||
fressnapftracker==0.2.0
|
||||
fressnapftracker==0.2.1
|
||||
|
||||
# homeassistant.components.fritz
|
||||
# homeassistant.components.fritzbox_callmonitor
|
||||
@@ -1172,7 +1172,7 @@ habluetooth==5.8.0
|
||||
hanna-cloud==0.0.7
|
||||
|
||||
# homeassistant.components.cloud
|
||||
hass-nabucasa==1.7.0
|
||||
hass-nabucasa==1.9.0
|
||||
|
||||
# homeassistant.components.splunk
|
||||
hass-splunk==0.1.1
|
||||
@@ -1216,7 +1216,7 @@ hole==0.9.0
|
||||
holidays==0.84
|
||||
|
||||
# homeassistant.components.frontend
|
||||
home-assistant-frontend==20260107.0
|
||||
home-assistant-frontend==20260107.1
|
||||
|
||||
# homeassistant.components.conversation
|
||||
home-assistant-intents==2026.1.6
|
||||
@@ -1684,7 +1684,7 @@ openwrt-luci-rpc==1.1.17
|
||||
openwrt-ubus-rpc==0.0.2
|
||||
|
||||
# homeassistant.components.opower
|
||||
opower==0.16.1
|
||||
opower==0.16.2
|
||||
|
||||
# homeassistant.components.oralb
|
||||
oralb-ble==1.0.2
|
||||
@@ -2481,7 +2481,7 @@ python-awair==0.2.5
|
||||
python-blockchain-api==0.0.2
|
||||
|
||||
# homeassistant.components.bsblan
|
||||
python-bsblan==3.1.6
|
||||
python-bsblan==4.1.0
|
||||
|
||||
# homeassistant.components.citybikes
|
||||
python-citybikes==0.3.3
|
||||
@@ -2520,7 +2520,7 @@ python-google-weather-api==0.0.4
|
||||
python-homeassistant-analytics==0.9.0
|
||||
|
||||
# homeassistant.components.homewizard
|
||||
python-homewizard-energy==10.0.0
|
||||
python-homewizard-energy==10.0.1
|
||||
|
||||
# homeassistant.components.hp_ilo
|
||||
python-hpilo==4.4.3
|
||||
@@ -2575,7 +2575,7 @@ python-overseerr==0.8.0
|
||||
python-picnic-api2==1.3.1
|
||||
|
||||
# homeassistant.components.pooldose
|
||||
python-pooldose==0.8.1
|
||||
python-pooldose==0.8.2
|
||||
|
||||
# homeassistant.components.rabbitair
|
||||
python-rabbitair==0.0.8
|
||||
@@ -3081,7 +3081,7 @@ typedmonarchmoney==0.4.4
|
||||
uasiren==0.0.1
|
||||
|
||||
# homeassistant.components.unifiprotect
|
||||
uiprotect==8.0.0
|
||||
uiprotect==8.1.1
|
||||
|
||||
# homeassistant.components.landisgyr_heat_meter
|
||||
ultraheat-api==0.5.7
|
||||
@@ -3116,7 +3116,7 @@ uvcclient==0.12.1
|
||||
vacuum-map-parser-roborock==0.1.4
|
||||
|
||||
# homeassistant.components.vallox
|
||||
vallox-websocket-api==5.3.0
|
||||
vallox-websocket-api==6.0.0
|
||||
|
||||
# homeassistant.components.vegehub
|
||||
vegehub==0.1.26
|
||||
@@ -3219,7 +3219,7 @@ wyoming==1.7.2
|
||||
xiaomi-ble==1.4.1
|
||||
|
||||
# homeassistant.components.knx
|
||||
xknx==3.13.0
|
||||
xknx==3.14.0
|
||||
|
||||
# homeassistant.components.knx
|
||||
xknxproject==3.8.2
|
||||
|
||||
20
requirements_test_all.txt
generated
20
requirements_test_all.txt
generated
@@ -890,7 +890,7 @@ forecast-solar==4.2.0
|
||||
freebox-api==1.2.2
|
||||
|
||||
# homeassistant.components.fressnapf_tracker
|
||||
fressnapftracker==0.2.0
|
||||
fressnapftracker==0.2.1
|
||||
|
||||
# homeassistant.components.fritz
|
||||
# homeassistant.components.fritzbox_callmonitor
|
||||
@@ -1042,7 +1042,7 @@ habluetooth==5.8.0
|
||||
hanna-cloud==0.0.7
|
||||
|
||||
# homeassistant.components.cloud
|
||||
hass-nabucasa==1.7.0
|
||||
hass-nabucasa==1.9.0
|
||||
|
||||
# homeassistant.components.assist_satellite
|
||||
# homeassistant.components.conversation
|
||||
@@ -1074,7 +1074,7 @@ hole==0.9.0
|
||||
holidays==0.84
|
||||
|
||||
# homeassistant.components.frontend
|
||||
home-assistant-frontend==20260107.0
|
||||
home-assistant-frontend==20260107.1
|
||||
|
||||
# homeassistant.components.conversation
|
||||
home-assistant-intents==2026.1.6
|
||||
@@ -1458,7 +1458,7 @@ openrgb-python==0.3.6
|
||||
openwebifpy==4.3.1
|
||||
|
||||
# homeassistant.components.opower
|
||||
opower==0.16.1
|
||||
opower==0.16.2
|
||||
|
||||
# homeassistant.components.oralb
|
||||
oralb-ble==1.0.2
|
||||
@@ -2098,7 +2098,7 @@ python-MotionMount==2.3.0
|
||||
python-awair==0.2.5
|
||||
|
||||
# homeassistant.components.bsblan
|
||||
python-bsblan==3.1.6
|
||||
python-bsblan==4.1.0
|
||||
|
||||
# homeassistant.components.ecobee
|
||||
python-ecobee-api==0.3.2
|
||||
@@ -2116,7 +2116,7 @@ python-google-weather-api==0.0.4
|
||||
python-homeassistant-analytics==0.9.0
|
||||
|
||||
# homeassistant.components.homewizard
|
||||
python-homewizard-energy==10.0.0
|
||||
python-homewizard-energy==10.0.1
|
||||
|
||||
# homeassistant.components.izone
|
||||
python-izone==1.2.9
|
||||
@@ -2165,7 +2165,7 @@ python-overseerr==0.8.0
|
||||
python-picnic-api2==1.3.1
|
||||
|
||||
# homeassistant.components.pooldose
|
||||
python-pooldose==0.8.1
|
||||
python-pooldose==0.8.2
|
||||
|
||||
# homeassistant.components.rabbitair
|
||||
python-rabbitair==0.0.8
|
||||
@@ -2575,7 +2575,7 @@ typedmonarchmoney==0.4.4
|
||||
uasiren==0.0.1
|
||||
|
||||
# homeassistant.components.unifiprotect
|
||||
uiprotect==8.0.0
|
||||
uiprotect==8.1.1
|
||||
|
||||
# homeassistant.components.landisgyr_heat_meter
|
||||
ultraheat-api==0.5.7
|
||||
@@ -2604,7 +2604,7 @@ uvcclient==0.12.1
|
||||
vacuum-map-parser-roborock==0.1.4
|
||||
|
||||
# homeassistant.components.vallox
|
||||
vallox-websocket-api==5.3.0
|
||||
vallox-websocket-api==6.0.0
|
||||
|
||||
# homeassistant.components.vegehub
|
||||
vegehub==0.1.26
|
||||
@@ -2692,7 +2692,7 @@ wyoming==1.7.2
|
||||
xiaomi-ble==1.4.1
|
||||
|
||||
# homeassistant.components.knx
|
||||
xknx==3.13.0
|
||||
xknx==3.14.0
|
||||
|
||||
# homeassistant.components.knx
|
||||
xknxproject==3.8.2
|
||||
|
||||
@@ -1390,7 +1390,6 @@ INTEGRATIONS_WITHOUT_SCALE = [
|
||||
"freebox",
|
||||
"freedns",
|
||||
"freedompro",
|
||||
"fritz",
|
||||
"fritzbox",
|
||||
"fritzbox_callmonitor",
|
||||
"frontier_silicon",
|
||||
@@ -2000,7 +1999,6 @@ INTEGRATIONS_WITHOUT_SCALE = [
|
||||
"touchline",
|
||||
"touchline_sl",
|
||||
"tplink_lte",
|
||||
"tplink_omada",
|
||||
"traccar",
|
||||
"traccar_server",
|
||||
"tractive",
|
||||
|
||||
@@ -24,7 +24,12 @@ def gather_info(arguments) -> Info:
|
||||
info = _gather_info(
|
||||
{
|
||||
"domain": {
|
||||
"prompt": "What is the domain?",
|
||||
"prompt": (
|
||||
"""What is the domain?
|
||||
|
||||
Hint: The domain is a short name consisting of characters and underscores.
|
||||
This domain has to be unique, cannot be changed, and has to match the directory name of the integration."""
|
||||
),
|
||||
"validators": [
|
||||
CHECK_EMPTY,
|
||||
[
|
||||
@@ -72,13 +77,8 @@ def gather_new_integration(determine_auth: bool) -> Info:
|
||||
},
|
||||
"codeowner": {
|
||||
"prompt": "What is your GitHub handle?",
|
||||
"validators": [
|
||||
CHECK_EMPTY,
|
||||
[
|
||||
'GitHub handles need to start with an "@"',
|
||||
lambda value: value.startswith("@"),
|
||||
],
|
||||
],
|
||||
"validators": [CHECK_EMPTY],
|
||||
"converter": lambda value: value if value.startswith("@") else f"@{value}",
|
||||
},
|
||||
"requirement": {
|
||||
"prompt": "What PyPI package and version do you depend on? Leave blank for none.",
|
||||
|
||||
@@ -172,6 +172,90 @@ class StateDescription(TypedDict):
|
||||
count: int
|
||||
|
||||
|
||||
class ConditionStateDescription(TypedDict):
|
||||
"""Test state and expected service call count."""
|
||||
|
||||
included: _StateDescription
|
||||
excluded: _StateDescription
|
||||
condition_true: bool
|
||||
state_valid: bool
|
||||
|
||||
|
||||
def parametrize_condition_states(
|
||||
*,
|
||||
condition: str,
|
||||
condition_options: dict[str, Any] | None = None,
|
||||
target_states: list[str | None | tuple[str | None, dict]],
|
||||
other_states: list[str | None | tuple[str | None, dict]],
|
||||
additional_attributes: dict | None = None,
|
||||
) -> list[tuple[str, dict[str, Any], list[ConditionStateDescription]]]:
|
||||
"""Parametrize states and expected service call counts.
|
||||
|
||||
The target_states and other_states iterables are either iterables of
|
||||
states or iterables of (state, attributes) tuples.
|
||||
|
||||
Returns a list of tuples with (condition, condition options, list of states),
|
||||
where states is a list of ConditionStateDescription dicts.
|
||||
"""
|
||||
|
||||
additional_attributes = additional_attributes or {}
|
||||
condition_options = condition_options or {}
|
||||
|
||||
def state_with_attributes(
|
||||
state: str | None | tuple[str | None, dict],
|
||||
condition_true: bool,
|
||||
state_valid: bool,
|
||||
) -> ConditionStateDescription:
|
||||
"""Return (state, attributes) dict."""
|
||||
if isinstance(state, str) or state is None:
|
||||
return {
|
||||
"included": {
|
||||
"state": state,
|
||||
"attributes": additional_attributes,
|
||||
},
|
||||
"excluded": {
|
||||
"state": state,
|
||||
"attributes": {},
|
||||
},
|
||||
"condition_true": condition_true,
|
||||
"state_valid": state_valid,
|
||||
}
|
||||
return {
|
||||
"included": {
|
||||
"state": state[0],
|
||||
"attributes": state[1] | additional_attributes,
|
||||
},
|
||||
"excluded": {
|
||||
"state": state[0],
|
||||
"attributes": state[1],
|
||||
},
|
||||
"condition_true": condition_true,
|
||||
"state_valid": state_valid,
|
||||
}
|
||||
|
||||
return [
|
||||
(
|
||||
condition,
|
||||
condition_options,
|
||||
list(
|
||||
itertools.chain(
|
||||
(state_with_attributes(None, False, False),),
|
||||
(state_with_attributes(STATE_UNAVAILABLE, False, False),),
|
||||
(state_with_attributes(STATE_UNKNOWN, False, False),),
|
||||
(
|
||||
state_with_attributes(other_state, False, True)
|
||||
for other_state in other_states
|
||||
),
|
||||
(
|
||||
state_with_attributes(target_state, True, True)
|
||||
for target_state in target_states
|
||||
),
|
||||
)
|
||||
),
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def parametrize_trigger_states(
|
||||
*,
|
||||
trigger: str,
|
||||
@@ -202,7 +286,7 @@ def parametrize_trigger_states(
|
||||
|
||||
def state_with_attributes(
|
||||
state: str | None | tuple[str | None, dict], count: int
|
||||
) -> dict:
|
||||
) -> StateDescription:
|
||||
"""Return (state, attributes) dict."""
|
||||
if isinstance(state, str) or state is None:
|
||||
return {
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from aioairzone.common import OperationMode
|
||||
from aioairzone.common import OperationMode, QAdapt
|
||||
from aioairzone.const import (
|
||||
API_COLD_ANGLE,
|
||||
API_DATA,
|
||||
API_HEAT_ANGLE,
|
||||
API_MODE,
|
||||
API_Q_ADAPT,
|
||||
API_SLEEP,
|
||||
API_SYSTEM_ID,
|
||||
API_ZONE_ID,
|
||||
@@ -17,7 +18,7 @@ import pytest
|
||||
from homeassistant.components.select import ATTR_OPTIONS, DOMAIN as SELECT_DOMAIN
|
||||
from homeassistant.const import ATTR_ENTITY_ID, ATTR_OPTION, SERVICE_SELECT_OPTION
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
|
||||
|
||||
from .util import async_init_integration
|
||||
|
||||
@@ -27,6 +28,11 @@ async def test_airzone_create_selects(hass: HomeAssistant) -> None:
|
||||
|
||||
await async_init_integration(hass)
|
||||
|
||||
# Systems
|
||||
state = hass.states.get("select.system_1_q_adapt")
|
||||
assert state.state == "standard"
|
||||
|
||||
# Zones
|
||||
state = hass.states.get("select.despacho_cold_angle")
|
||||
assert state.state == "90deg"
|
||||
|
||||
@@ -95,6 +101,71 @@ async def test_airzone_create_selects(hass: HomeAssistant) -> None:
|
||||
assert state.state == "off"
|
||||
|
||||
|
||||
async def test_airzone_select_sys_qadapt(hass: HomeAssistant) -> None:
|
||||
"""Test select system Q-Adapt."""
|
||||
|
||||
await async_init_integration(hass)
|
||||
|
||||
put_q_adapt = {
|
||||
API_DATA: {
|
||||
API_SYSTEM_ID: 1,
|
||||
API_Q_ADAPT: QAdapt.SILENCE,
|
||||
}
|
||||
}
|
||||
|
||||
with pytest.raises(ServiceValidationError):
|
||||
await hass.services.async_call(
|
||||
SELECT_DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
{
|
||||
ATTR_ENTITY_ID: "select.system_1_q_adapt",
|
||||
ATTR_OPTION: "Invalid",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.airzone.AirzoneLocalApi.put_hvac",
|
||||
return_value=put_q_adapt,
|
||||
):
|
||||
await hass.services.async_call(
|
||||
SELECT_DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
{
|
||||
ATTR_ENTITY_ID: "select.system_1_q_adapt",
|
||||
ATTR_OPTION: "silence",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
state = hass.states.get("select.system_1_q_adapt")
|
||||
assert state.state == "silence"
|
||||
|
||||
put_q_adapt = {
|
||||
API_DATA: {
|
||||
API_SYSTEM_ID: 2,
|
||||
API_Q_ADAPT: QAdapt.SILENCE,
|
||||
}
|
||||
}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.airzone.AirzoneLocalApi.put_hvac",
|
||||
return_value=put_q_adapt,
|
||||
),
|
||||
pytest.raises(HomeAssistantError),
|
||||
):
|
||||
await hass.services.async_call(
|
||||
SELECT_DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
{
|
||||
ATTR_ENTITY_ID: "select.system_1_q_adapt",
|
||||
ATTR_OPTION: "silence",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
|
||||
async def test_airzone_select_sleep(hass: HomeAssistant) -> None:
|
||||
"""Test select sleep."""
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ CONF_DATA: Final = {CONF_HOST: "test", CONF_PORT: 1234}
|
||||
|
||||
MOCK_STATUS: Final = {
|
||||
"APC": "001,038,0985",
|
||||
"DATE": "1970-01-01 00:00:00 0000",
|
||||
"DATE": "1970-01-01 00:00:00 +0000",
|
||||
"VERSION": "3.14.14 (31 May 2016) unknown",
|
||||
"CABLE": "USB Cable",
|
||||
"DRIVER": "USB UPS Driver",
|
||||
@@ -19,6 +19,7 @@ MOCK_STATUS: Final = {
|
||||
"APCMODEL": "Back-UPS ES 600",
|
||||
"MODEL": "Back-UPS ES 600",
|
||||
"STATUS": "ONLINE",
|
||||
"STARTTIME": "2006-01-01 00:00:00 +0500",
|
||||
"LINEV": "124.0 Volts",
|
||||
"LOADPCT": "14.0 Percent",
|
||||
"BCHARGE": "100.0 Percent",
|
||||
@@ -36,11 +37,11 @@ MOCK_STATUS: Final = {
|
||||
"OUTCURNT": "0.88 Amps",
|
||||
"LASTXFER": "Automatic or explicit self test",
|
||||
"NUMXFERS": "1",
|
||||
"XONBATT": "1970-01-01 00:00:00 0000",
|
||||
"XONBATT": "1970-01-01 00:00:00 +0000",
|
||||
"TONBATT": "0 Seconds",
|
||||
"CUMONBATT": "8 Seconds",
|
||||
"XOFFBATT": "1970-01-01 00:00:00 0000",
|
||||
"LASTSTEST": "1970-01-01 00:00:00 0000",
|
||||
"XOFFBATT": "1970-01-01 00:00:00 +0000",
|
||||
"LASTSTEST": "1970-01-01 00:00:00 +0000",
|
||||
"SELFTEST": "NO",
|
||||
"STESTI": "7 days",
|
||||
"STATFLAG": "0x05000008",
|
||||
@@ -50,7 +51,8 @@ MOCK_STATUS: Final = {
|
||||
"NOMBATTV": "12.0 Volts",
|
||||
"NOMPOWER": "330 Watts",
|
||||
"FIRMWARE": "928.a8 .D USB FW:a8",
|
||||
"END APC": "1970-01-01 00:00:00 0000",
|
||||
"MASTERUPD": "1970-01-01 00:00:00 +0000",
|
||||
"END APC": "1970-01-01 00:00:00 +0000",
|
||||
}
|
||||
|
||||
# Minimal status adapted from http://www.apcupsd.org/manual/manual.html#apcaccess-test.
|
||||
@@ -58,13 +60,13 @@ MOCK_STATUS: Final = {
|
||||
# of the integration to handle such cases.
|
||||
MOCK_MINIMAL_STATUS: Final = {
|
||||
"APC": "001,012,0319",
|
||||
"DATE": "1970-01-01 00:00:00 0000",
|
||||
"DATE": "1970-01-01 00:00:00 +0000",
|
||||
"RELEASE": "3.8.5",
|
||||
"CABLE": "APC Cable 940-0128A",
|
||||
"UPSMODE": "Stand Alone",
|
||||
"STARTTIME": "1970-01-01 00:00:00 0000",
|
||||
"STARTTIME": "1970-01-01 00:00:00 +0000",
|
||||
"LINEFAIL": "OK",
|
||||
"BATTSTAT": "OK",
|
||||
"STATFLAG": "0x008",
|
||||
"END APC": "1970-01-01 00:00:00 0000",
|
||||
"END APC": "1970-01-01 00:00:00 +0000",
|
||||
}
|
||||
|
||||
@@ -9,17 +9,18 @@
|
||||
'BCHARGE': '100.0 Percent',
|
||||
'CABLE': 'USB Cable',
|
||||
'CUMONBATT': '8 Seconds',
|
||||
'DATE': '1970-01-01 00:00:00 0000',
|
||||
'DATE': '1970-01-01 00:00:00 +0000',
|
||||
'DRIVER': 'USB UPS Driver',
|
||||
'END APC': '1970-01-01 00:00:00 0000',
|
||||
'END APC': '1970-01-01 00:00:00 +0000',
|
||||
'FIRMWARE': '928.a8 .D USB FW:a8',
|
||||
'HITRANS': '139.0 Volts',
|
||||
'ITEMP': '34.6 C Internal',
|
||||
'LASTSTEST': '1970-01-01 00:00:00 0000',
|
||||
'LASTSTEST': '1970-01-01 00:00:00 +0000',
|
||||
'LASTXFER': 'Automatic or explicit self test',
|
||||
'LINEV': '124.0 Volts',
|
||||
'LOADPCT': '14.0 Percent',
|
||||
'LOTRANS': '92.0 Volts',
|
||||
'MASTERUPD': '1970-01-01 00:00:00 +0000',
|
||||
'MAXTIME': '0 Seconds',
|
||||
'MBATTCHG': '5 Percent',
|
||||
'MINTIMEL': '3 Minutes',
|
||||
@@ -33,6 +34,7 @@
|
||||
'SELFTEST': 'NO',
|
||||
'SENSE': 'Medium',
|
||||
'SERIALNO': '**REDACTED**',
|
||||
'STARTTIME': '2006-01-01 00:00:00 +0500',
|
||||
'STATFLAG': '0x05000008',
|
||||
'STATUS': 'ONLINE',
|
||||
'STESTI': '7 days',
|
||||
@@ -41,7 +43,7 @@
|
||||
'UPSMODE': 'Stand Alone',
|
||||
'UPSNAME': 'MyUPS',
|
||||
'VERSION': '3.14.14 (31 May 2016) unknown',
|
||||
'XOFFBATT': '1970-01-01 00:00:00 0000',
|
||||
'XONBATT': '1970-01-01 00:00:00 0000',
|
||||
'XOFFBATT': '1970-01-01 00:00:00 +0000',
|
||||
'XONBATT': '1970-01-01 00:00:00 +0000',
|
||||
})
|
||||
# ---
|
||||
|
||||
@@ -737,7 +737,7 @@
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '1970-01-01 00:00:00 0000',
|
||||
'state': '1970-01-01 00:00:00 +0000',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor[sensor.myups_driver-entry]
|
||||
@@ -971,7 +971,7 @@
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Last self-test',
|
||||
'platform': 'apcupsd',
|
||||
@@ -986,6 +986,7 @@
|
||||
# name: test_sensor[sensor.myups_last_self_test-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'timestamp',
|
||||
'friendly_name': 'MyUPS Last self-test',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
@@ -993,7 +994,7 @@
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '1970-01-01 00:00:00 0000',
|
||||
'state': '1970-01-01T00:00:00+00:00',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor[sensor.myups_last_transfer-entry]
|
||||
@@ -1096,6 +1097,55 @@
|
||||
'state': '14.0',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor[sensor.myups_master_update-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
|
||||
'entity_id': 'sensor.myups_master_update',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Master update',
|
||||
'platform': 'apcupsd',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'master_update',
|
||||
'unique_id': 'XXXXXXXXXXXX_masterupd',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor[sensor.myups_master_update-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'timestamp',
|
||||
'friendly_name': 'MyUPS Master update',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.myups_master_update',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '1970-01-01T00:00:00+00:00',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor[sensor.myups_mode-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
@@ -1745,6 +1795,55 @@
|
||||
'state': '3',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor[sensor.myups_startup_time-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
|
||||
'entity_id': 'sensor.myups_startup_time',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Startup time',
|
||||
'platform': 'apcupsd',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'startup_time',
|
||||
'unique_id': 'XXXXXXXXXXXX_starttime',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor[sensor.myups_startup_time-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'timestamp',
|
||||
'friendly_name': 'MyUPS Startup time',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.myups_startup_time',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '2005-12-31T19:00:00+00:00',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor[sensor.myups_status-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
@@ -1886,7 +1985,7 @@
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '1970-01-01 00:00:00 0000',
|
||||
'state': '1970-01-01 00:00:00 +0000',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor[sensor.myups_status_flag-entry]
|
||||
@@ -2179,7 +2278,7 @@
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Transfer from battery',
|
||||
'platform': 'apcupsd',
|
||||
@@ -2194,6 +2293,7 @@
|
||||
# name: test_sensor[sensor.myups_transfer_from_battery-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'timestamp',
|
||||
'friendly_name': 'MyUPS Transfer from battery',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
@@ -2201,7 +2301,7 @@
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '1970-01-01 00:00:00 0000',
|
||||
'state': '1970-01-01T00:00:00+00:00',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor[sensor.myups_transfer_high-entry]
|
||||
@@ -2333,7 +2433,7 @@
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Transfer to battery',
|
||||
'platform': 'apcupsd',
|
||||
@@ -2348,6 +2448,7 @@
|
||||
# name: test_sensor[sensor.myups_transfer_to_battery-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'timestamp',
|
||||
'friendly_name': 'MyUPS Transfer to battery',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
@@ -2355,6 +2456,6 @@
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '1970-01-01 00:00:00 0000',
|
||||
'state': '1970-01-01T00:00:00+00:00',
|
||||
})
|
||||
# ---
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from datetime import timedelta
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import dateutil.parser
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
@@ -151,13 +152,17 @@ async def test_sensor_unknown(
|
||||
|
||||
# Simulate an event (a self test) such that "LASTSTEST" field is being reported, the state of
|
||||
# the sensor should be properly updated with the corresponding value.
|
||||
last_self_test_value = "1970-01-01 00:00:00 +0000"
|
||||
mock_request_status.return_value = MOCK_MINIMAL_STATUS | {
|
||||
"LASTSTEST": "1970-01-01 00:00:00 0000"
|
||||
"LASTSTEST": last_self_test_value
|
||||
}
|
||||
future = utcnow() + timedelta(minutes=2)
|
||||
async_fire_time_changed(hass, future)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(last_self_test_id).state == "1970-01-01 00:00:00 0000"
|
||||
assert (
|
||||
hass.states.get(last_self_test_id).state
|
||||
== dateutil.parser.parse(last_self_test_value).isoformat()
|
||||
)
|
||||
|
||||
# Simulate another event (e.g., daemon restart) such that "LASTSTEST" is no longer reported.
|
||||
mock_request_status.return_value = MOCK_MINIMAL_STATUS
|
||||
|
||||
@@ -159,6 +159,30 @@ async def test_climate_hvac_mode_none_value(
|
||||
assert state.state == "unknown"
|
||||
|
||||
|
||||
async def test_climate_hvac_mode_object_none(
|
||||
hass: HomeAssistant,
|
||||
mock_bsblan: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test climate entity when hvac_mode object itself is None."""
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.CLIMATE])
|
||||
|
||||
# Set hvac_mode to None (the object itself, not just the value)
|
||||
mock_bsblan.state.return_value.hvac_mode = None
|
||||
|
||||
freezer.tick(timedelta(minutes=1))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# State should be unknown when hvac_mode object is None
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state is not None
|
||||
assert state.state == "unknown"
|
||||
# preset_mode should be "none" when hvac_mode object is None
|
||||
assert state.attributes["preset_mode"] == PRESET_NONE
|
||||
|
||||
|
||||
async def test_climate_hvac_mode_string_fallback(
|
||||
hass: HomeAssistant,
|
||||
mock_bsblan: AsyncMock,
|
||||
|
||||
@@ -245,6 +245,7 @@ async def cloud_prefs(hass: HomeAssistant) -> CloudPreferences:
|
||||
async def mock_cloud_setup(hass: HomeAssistant) -> None:
|
||||
"""Set up the cloud."""
|
||||
await mock_cloud(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
@@ -130,7 +130,6 @@ async def setup_cloud_fixture(hass: HomeAssistant, cloud: MagicMock) -> None:
|
||||
"relayer_server": "relayer",
|
||||
"acme_server": "cert-server",
|
||||
"api_server": "api-test.example.com",
|
||||
"accounts_server": "api-test.hass.io",
|
||||
"google_actions": {"filter": {"include_domains": "light"}},
|
||||
"alexa": {
|
||||
"filter": {"include_entities": ["light.kitchen", "switch.ac"]}
|
||||
|
||||
@@ -45,7 +45,6 @@ async def test_constructor_loads_info_from_config(hass: HomeAssistant) -> None:
|
||||
"region": "test-region",
|
||||
"api_server": "test-api-server",
|
||||
"relayer_server": "test-relayer-server",
|
||||
"accounts_server": "test-acounts-server",
|
||||
"acme_server": "test-acme-server",
|
||||
"remotestate_server": "test-remotestate-server",
|
||||
"discovery_service_actions": {
|
||||
@@ -63,7 +62,6 @@ async def test_constructor_loads_info_from_config(hass: HomeAssistant) -> None:
|
||||
assert cl.region == "test-region"
|
||||
assert cl.relayer_server == "test-relayer-server"
|
||||
assert cl.iot.ws_server_url == "wss://test-relayer-server/websocket"
|
||||
assert cl.accounts_server == "test-acounts-server"
|
||||
assert cl.acme_server == "test-acme-server"
|
||||
assert cl.api_server == "test-api-server"
|
||||
assert cl.remotestate_server == "test-remotestate-server"
|
||||
|
||||
@@ -48,7 +48,7 @@ async def test_create_repair_issues_at_startup_if_logged_in(
|
||||
) -> None:
|
||||
"""Test that we create repair issue at startup if we are logged in."""
|
||||
aioclient_mock.get(
|
||||
"https://accounts.nabucasa.com/payments/subscription_info",
|
||||
"https://api.nabucasa.com/account/payments/subscription_info",
|
||||
json={"provider": "legacy"},
|
||||
)
|
||||
|
||||
@@ -88,11 +88,11 @@ async def test_legacy_subscription_repair_flow(
|
||||
) -> None:
|
||||
"""Test desired flow of the fix flow for legacy subscription."""
|
||||
aioclient_mock.get(
|
||||
"https://accounts.nabucasa.com/payments/subscription_info",
|
||||
"https://api.nabucasa.com/account/payments/subscription_info",
|
||||
json={"provider": None},
|
||||
)
|
||||
aioclient_mock.post(
|
||||
"https://accounts.nabucasa.com/payments/migrate_paypal_agreement",
|
||||
"https://api.nabucasa.com/account/payments/migrate_paypal_agreement",
|
||||
json={"url": "https://paypal.com"},
|
||||
)
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
async def mocked_cloud_object(hass: HomeAssistant) -> Cloud:
|
||||
"""Mock cloud object."""
|
||||
return Mock(
|
||||
accounts_server="accounts.nabucasa.com",
|
||||
auth=Mock(async_check_token=AsyncMock()),
|
||||
websession=async_get_clientsession(hass),
|
||||
payments=Mock(
|
||||
|
||||
@@ -345,10 +345,10 @@ async def test_get_tts_audio_logged_out(
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("mock_process_tts_side_effect"),
|
||||
"mock_process_tts_side_effect",
|
||||
[
|
||||
(None,),
|
||||
(VoiceError("Boom!"),),
|
||||
None,
|
||||
VoiceError("Boom!"),
|
||||
],
|
||||
)
|
||||
async def test_tts_entity(
|
||||
|
||||
@@ -823,6 +823,9 @@ async def _check_config_flow_result_translations(
|
||||
integration = flow.handler
|
||||
issue_id = flow.issue_id
|
||||
issue = ir.async_get(flow.hass).async_get_issue(integration, issue_id)
|
||||
if issue is None:
|
||||
# Issue was deleted mid-flow (e.g., config entry removed), skip check
|
||||
return
|
||||
key_prefix = f"{issue.translation_key}.fix_flow."
|
||||
description_placeholders = {
|
||||
# Both are used in issue translations, and description_placeholders
|
||||
|
||||
@@ -281,7 +281,7 @@
|
||||
'attribution': 'Data provided by Fitbit.com',
|
||||
'device_class': 'duration',
|
||||
'friendly_name': 'First L. Sleep time in bed',
|
||||
'icon': 'mdi:hotel',
|
||||
'icon': 'mdi:bed',
|
||||
'state_class': <SensorStateClass.TOTAL_INCREASING: 'total_increasing'>,
|
||||
'unit_of_measurement': <UnitOfTime.MINUTES: 'min'>,
|
||||
}),
|
||||
|
||||
@@ -50,7 +50,7 @@ async def test_user_flow_success(
|
||||
# Submit SMS code
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_SMS_CODE: 123456},
|
||||
{CONF_SMS_CODE: "0123456"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
@@ -107,7 +107,7 @@ async def test_user_flow_request_sms_code_errors(
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_SMS_CODE: 123456},
|
||||
{CONF_SMS_CODE: "0123456"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
@@ -142,7 +142,7 @@ async def test_user_flow_verify_phone_number_errors(
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_SMS_CODE: 999999},
|
||||
{CONF_SMS_CODE: "999999"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
@@ -153,7 +153,7 @@ async def test_user_flow_verify_phone_number_errors(
|
||||
mock_auth_client.verify_phone_number.side_effect = None
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_SMS_CODE: 123456},
|
||||
{CONF_SMS_CODE: "0123456"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
@@ -246,7 +246,7 @@ async def test_reauth_reconfigure_flow(
|
||||
# Submit SMS code
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_SMS_CODE: 123456},
|
||||
{CONF_SMS_CODE: "0123456"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
@@ -311,7 +311,7 @@ async def test_reauth_reconfigure_flow_invalid_phone_number(
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_SMS_CODE: 123456},
|
||||
{CONF_SMS_CODE: "0123456"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
@@ -358,7 +358,7 @@ async def test_reauth_reconfigure_flow_invalid_sms_code(
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_SMS_CODE: 999999},
|
||||
{CONF_SMS_CODE: "999999"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
@@ -369,7 +369,7 @@ async def test_reauth_reconfigure_flow_invalid_sms_code(
|
||||
mock_auth_client.verify_phone_number.side_effect = None
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_SMS_CODE: 123456},
|
||||
{CONF_SMS_CODE: "0123456"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
@@ -436,7 +436,7 @@ async def test_reauth_reconfigure_flow_invalid_user_id(
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_SMS_CODE: 123456},
|
||||
{CONF_SMS_CODE: "0123456"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
|
||||
@@ -208,7 +208,7 @@
|
||||
'domain': 'button',
|
||||
'entity_category': None,
|
||||
'entity_id': 'button.printer_wake_on_lan',
|
||||
'has_entity_name': False,
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
@@ -218,13 +218,13 @@
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': 'mdi:lan-pending',
|
||||
'original_name': 'printer Wake on LAN',
|
||||
'original_icon': None,
|
||||
'original_name': 'Wake on LAN',
|
||||
'platform': 'fritz',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': None,
|
||||
'translation_key': 'wake_on_lan',
|
||||
'unique_id': 'AA:BB:CC:00:11:22_wake_on_lan',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
@@ -233,7 +233,6 @@
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'printer Wake on LAN',
|
||||
'icon': 'mdi:lan-pending',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'button.printer_wake_on_lan',
|
||||
|
||||
@@ -111,7 +111,7 @@
|
||||
'domain': 'switch',
|
||||
'entity_category': <EntityCategory.CONFIG: 'config'>,
|
||||
'entity_id': 'switch.printer_internet_access',
|
||||
'has_entity_name': False,
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
@@ -121,13 +121,13 @@
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': 'mdi:router-wireless-settings',
|
||||
'original_name': 'printer Internet Access',
|
||||
'original_icon': None,
|
||||
'original_name': 'Internet access',
|
||||
'platform': 'fritz',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': None,
|
||||
'translation_key': 'internet_access',
|
||||
'unique_id': 'AA:BB:CC:00:11:22_internet_access',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
@@ -135,8 +135,7 @@
|
||||
# name: test_switch_setup[fc_data0][switch.printer_internet_access-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'printer Internet Access',
|
||||
'icon': 'mdi:router-wireless-settings',
|
||||
'friendly_name': 'printer Internet access',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'switch.printer_internet_access',
|
||||
@@ -258,7 +257,7 @@
|
||||
'domain': 'switch',
|
||||
'entity_category': <EntityCategory.CONFIG: 'config'>,
|
||||
'entity_id': 'switch.printer_internet_access',
|
||||
'has_entity_name': False,
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
@@ -268,13 +267,13 @@
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': 'mdi:router-wireless-settings',
|
||||
'original_name': 'printer Internet Access',
|
||||
'original_icon': None,
|
||||
'original_name': 'Internet access',
|
||||
'platform': 'fritz',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': None,
|
||||
'translation_key': 'internet_access',
|
||||
'unique_id': 'AA:BB:CC:00:11:22_internet_access',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
@@ -282,8 +281,7 @@
|
||||
# name: test_switch_setup[fc_data1][switch.printer_internet_access-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'printer Internet Access',
|
||||
'icon': 'mdi:router-wireless-settings',
|
||||
'friendly_name': 'printer Internet access',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'switch.printer_internet_access',
|
||||
@@ -405,7 +403,7 @@
|
||||
'domain': 'switch',
|
||||
'entity_category': <EntityCategory.CONFIG: 'config'>,
|
||||
'entity_id': 'switch.printer_internet_access',
|
||||
'has_entity_name': False,
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
@@ -415,13 +413,13 @@
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': 'mdi:router-wireless-settings',
|
||||
'original_name': 'printer Internet Access',
|
||||
'original_icon': None,
|
||||
'original_name': 'Internet access',
|
||||
'platform': 'fritz',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': None,
|
||||
'translation_key': 'internet_access',
|
||||
'unique_id': 'AA:BB:CC:00:11:22_internet_access',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
@@ -429,8 +427,7 @@
|
||||
# name: test_switch_setup[fc_data2][switch.printer_internet_access-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'printer Internet Access',
|
||||
'icon': 'mdi:router-wireless-settings',
|
||||
'friendly_name': 'printer Internet access',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'switch.printer_internet_access',
|
||||
@@ -558,7 +555,7 @@
|
||||
'domain': 'switch',
|
||||
'entity_category': <EntityCategory.CONFIG: 'config'>,
|
||||
'entity_id': 'switch.printer_internet_access',
|
||||
'has_entity_name': False,
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
@@ -568,13 +565,13 @@
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': 'mdi:router-wireless-settings',
|
||||
'original_name': 'printer Internet Access',
|
||||
'original_icon': None,
|
||||
'original_name': 'Internet access',
|
||||
'platform': 'fritz',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': None,
|
||||
'translation_key': 'internet_access',
|
||||
'unique_id': 'AA:BB:CC:00:11:22_internet_access',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
@@ -582,8 +579,7 @@
|
||||
# name: test_switch_setup[fc_data3][switch.printer_internet_access-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'printer Internet Access',
|
||||
'icon': 'mdi:router-wireless-settings',
|
||||
'friendly_name': 'printer Internet access',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'switch.printer_internet_access',
|
||||
|
||||
@@ -81,6 +81,42 @@
|
||||
"value": 1.2,
|
||||
"units": "PARTS_PER_BILLION"
|
||||
}
|
||||
},
|
||||
{
|
||||
"code": "nh3",
|
||||
"displayName": "NH3",
|
||||
"fullName": "Ammonia",
|
||||
"concentration": {
|
||||
"value": 81.41,
|
||||
"units": "PARTS_PER_BILLION"
|
||||
}
|
||||
},
|
||||
{
|
||||
"code": "no",
|
||||
"displayName": "NO",
|
||||
"fullName": "Nitrogen monoxide",
|
||||
"concentration": {
|
||||
"value": 0.62,
|
||||
"units": "PARTS_PER_BILLION"
|
||||
}
|
||||
},
|
||||
{
|
||||
"code": "nmhc",
|
||||
"displayName": "NMHC",
|
||||
"fullName": "Non-methane hydrocarbons",
|
||||
"concentration": {
|
||||
"value": 52.66,
|
||||
"units": "PARTS_PER_BILLION"
|
||||
}
|
||||
},
|
||||
{
|
||||
"code": "c6h6",
|
||||
"displayName": "C6H6",
|
||||
"fullName": "Benzene",
|
||||
"concentration": {
|
||||
"value": 0.24,
|
||||
"units": "MICROGRAMS_PER_CUBIC_METER"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,4 +1,110 @@
|
||||
# serializer version: 1
|
||||
# name: test_sensor_snapshot[sensor.home_ammonia-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.home_ammonia',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Ammonia',
|
||||
'platform': 'google_air_quality',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'ammonia',
|
||||
'unique_id': 'nh3_10.1_20.1',
|
||||
'unit_of_measurement': 'ppb',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor_snapshot[sensor.home_ammonia-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'attribution': 'Data provided by Google Air Quality',
|
||||
'friendly_name': 'Home Ammonia',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': 'ppb',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.home_ammonia',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '81.41',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor_snapshot[sensor.home_benzene-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.home_benzene',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Benzene',
|
||||
'platform': 'google_air_quality',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'benzene',
|
||||
'unique_id': 'c6h6_10.1_20.1',
|
||||
'unit_of_measurement': 'μg/m³',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor_snapshot[sensor.home_benzene-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'attribution': 'Data provided by Google Air Quality',
|
||||
'friendly_name': 'Home Benzene',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': 'μg/m³',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.home_benzene',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '0.24',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor_snapshot[sensor.home_carbon_monoxide-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
@@ -234,6 +340,112 @@
|
||||
'state': '14.18',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor_snapshot[sensor.home_nitrogen_monoxide-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.home_nitrogen_monoxide',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Nitrogen monoxide',
|
||||
'platform': 'google_air_quality',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'nitrogen_monoxide',
|
||||
'unique_id': 'no_10.1_20.1',
|
||||
'unit_of_measurement': 'ppb',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor_snapshot[sensor.home_nitrogen_monoxide-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'attribution': 'Data provided by Google Air Quality',
|
||||
'friendly_name': 'Home Nitrogen monoxide',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': 'ppb',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.home_nitrogen_monoxide',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '0.62',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor_snapshot[sensor.home_non_methane_hydrocarbons-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.home_non_methane_hydrocarbons',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Non-methane hydrocarbons',
|
||||
'platform': 'google_air_quality',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'non_methane_hydrocarbons',
|
||||
'unique_id': 'nmhc_10.1_20.1',
|
||||
'unit_of_measurement': 'ppb',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor_snapshot[sensor.home_non_methane_hydrocarbons-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'attribution': 'Data provided by Google Air Quality',
|
||||
'friendly_name': 'Home Non-methane hydrocarbons',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': 'ppb',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.home_non_methane_hydrocarbons',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '52.66',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor_snapshot[sensor.home_ozone-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
|
||||
30
tests/components/hdfury/snapshots/test_diagnostics.ambr
Normal file
30
tests/components/hdfury/snapshots/test_diagnostics.ambr
Normal file
@@ -0,0 +1,30 @@
|
||||
# serializer version: 1
|
||||
# name: test_diagnostics
|
||||
dict({
|
||||
'board': dict({
|
||||
'hostname': 'VRROOM-02',
|
||||
'ipaddress': '192.168.1.123',
|
||||
'pcbv': '3',
|
||||
'serial': '000123456789',
|
||||
'version': 'FW: 0.61',
|
||||
}),
|
||||
'config': dict({
|
||||
'autosw': '1',
|
||||
'htpcmode0': '0',
|
||||
'htpcmode1': '0',
|
||||
'htpcmode2': '0',
|
||||
'htpcmode3': '0',
|
||||
'iractive': '1',
|
||||
'macaddr': 'c7:1c:df:9d:f6:40',
|
||||
'mutetx0': '1',
|
||||
'mutetx1': '1',
|
||||
'oled': '1',
|
||||
'relay': '0',
|
||||
}),
|
||||
'info': dict({
|
||||
'opmode': '0',
|
||||
'portseltx0': '0',
|
||||
'portseltx1': '4',
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
31
tests/components/hdfury/test_diagnostics.py
Normal file
31
tests/components/hdfury/test_diagnostics.py
Normal file
@@ -0,0 +1,31 @@
|
||||
"""Tests for the HDFury diagnostics."""
|
||||
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.hdfury import PLATFORMS
|
||||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.helpers.entity_registry as er
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from tests.typing import ClientSessionGenerator
|
||||
|
||||
|
||||
async def test_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test HDFury diagnostics."""
|
||||
|
||||
await setup_integration(hass, mock_config_entry, PLATFORMS)
|
||||
|
||||
diagnostics = await get_diagnostics_for_config_entry(
|
||||
hass, hass_client, mock_config_entry
|
||||
)
|
||||
|
||||
assert diagnostics == snapshot
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Test light conditions."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
@@ -13,24 +14,18 @@ from homeassistant.const import (
|
||||
CONF_TARGET,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
STATE_UNAVAILABLE,
|
||||
STATE_UNKNOWN,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.components import (
|
||||
ConditionStateDescription,
|
||||
parametrize_condition_states,
|
||||
parametrize_target_entities,
|
||||
set_or_remove_state,
|
||||
target_entities,
|
||||
)
|
||||
|
||||
INVALID_STATES = [
|
||||
{"state": STATE_UNAVAILABLE, "attributes": {}},
|
||||
{"state": STATE_UNKNOWN, "attributes": {}},
|
||||
{"state": None, "attributes": {}},
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
|
||||
def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
|
||||
@@ -76,15 +71,15 @@ async def setup_automation_with_light_condition(
|
||||
)
|
||||
|
||||
|
||||
async def has_call_after_trigger(
|
||||
async def has_single_call_after_trigger(
|
||||
hass: HomeAssistant, service_calls: list[ServiceCall]
|
||||
) -> bool:
|
||||
"""Check if there are service calls after the trigger event."""
|
||||
"""Check if there is a single service call after the trigger event."""
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
has_calls = len(service_calls) == 1
|
||||
num_calls = len(service_calls)
|
||||
service_calls.clear()
|
||||
return has_calls
|
||||
return num_calls == 1
|
||||
|
||||
|
||||
@pytest.fixture(name="enable_experimental_triggers_conditions")
|
||||
@@ -125,17 +120,17 @@ async def test_light_conditions_gated_by_labs_flag(
|
||||
parametrize_target_entities("light"),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("condition", "target_state", "other_state"),
|
||||
("condition", "condition_options", "states"),
|
||||
[
|
||||
(
|
||||
"light.is_on",
|
||||
{"state": STATE_ON, "attributes": {}},
|
||||
{"state": STATE_OFF, "attributes": {}},
|
||||
*parametrize_condition_states(
|
||||
condition="light.is_on",
|
||||
target_states=[STATE_ON],
|
||||
other_states=[STATE_OFF],
|
||||
),
|
||||
(
|
||||
"light.is_off",
|
||||
{"state": STATE_OFF, "attributes": {}},
|
||||
{"state": STATE_ON, "attributes": {}},
|
||||
*parametrize_condition_states(
|
||||
condition="light.is_off",
|
||||
target_states=[STATE_OFF],
|
||||
other_states=[STATE_ON],
|
||||
),
|
||||
],
|
||||
)
|
||||
@@ -148,15 +143,15 @@ async def test_light_state_condition_behavior_any(
|
||||
entity_id: str,
|
||||
entities_in_target: int,
|
||||
condition: str,
|
||||
target_state: str,
|
||||
other_state: str,
|
||||
condition_options: dict[str, Any],
|
||||
states: list[ConditionStateDescription],
|
||||
) -> None:
|
||||
"""Test the light state condition with the 'any' behavior."""
|
||||
other_entity_ids = set(target_lights) - {entity_id}
|
||||
|
||||
# Set all lights, including the tested light, to the initial state
|
||||
for eid in target_lights:
|
||||
set_or_remove_state(hass, eid, other_state)
|
||||
set_or_remove_state(hass, eid, states[0]["included"])
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await setup_automation_with_light_condition(
|
||||
@@ -167,38 +162,29 @@ async def test_light_state_condition_behavior_any(
|
||||
)
|
||||
|
||||
# Set state for switches to ensure that they don't impact the condition
|
||||
for eid in target_switches:
|
||||
set_or_remove_state(hass, eid, other_state)
|
||||
await hass.async_block_till_done()
|
||||
assert not await has_call_after_trigger(hass, service_calls)
|
||||
for state in states:
|
||||
for eid in target_switches:
|
||||
set_or_remove_state(hass, eid, state["included"])
|
||||
await hass.async_block_till_done()
|
||||
assert not await has_single_call_after_trigger(hass, service_calls)
|
||||
|
||||
for eid in target_switches:
|
||||
set_or_remove_state(hass, eid, target_state)
|
||||
await hass.async_block_till_done()
|
||||
assert not await has_call_after_trigger(hass, service_calls)
|
||||
|
||||
# Set one light to the condition state -> condition pass
|
||||
set_or_remove_state(hass, entity_id, target_state)
|
||||
assert await has_call_after_trigger(hass, service_calls)
|
||||
|
||||
# Set all remaining lights to the condition state -> condition pass
|
||||
for eid in other_entity_ids:
|
||||
set_or_remove_state(hass, eid, target_state)
|
||||
assert await has_call_after_trigger(hass, service_calls)
|
||||
|
||||
for invalid_state in INVALID_STATES:
|
||||
# Set one light to the invalid state -> condition pass if there are
|
||||
# other lights in the condition state
|
||||
set_or_remove_state(hass, entity_id, invalid_state)
|
||||
assert await has_call_after_trigger(hass, service_calls) == bool(
|
||||
entities_in_target - 1
|
||||
for state in states:
|
||||
included_state = state["included"]
|
||||
set_or_remove_state(hass, entity_id, included_state)
|
||||
await hass.async_block_till_done()
|
||||
assert (
|
||||
await has_single_call_after_trigger(hass, service_calls)
|
||||
== state["condition_true"]
|
||||
)
|
||||
|
||||
for invalid_state in INVALID_STATES:
|
||||
# Set all lights to invalid state -> condition fail
|
||||
for eid in other_entity_ids:
|
||||
set_or_remove_state(hass, eid, invalid_state)
|
||||
assert not await has_call_after_trigger(hass, service_calls)
|
||||
# Check if changing other lights also passes the condition
|
||||
for other_entity_id in other_entity_ids:
|
||||
set_or_remove_state(hass, other_entity_id, included_state)
|
||||
await hass.async_block_till_done()
|
||||
assert (
|
||||
await has_single_call_after_trigger(hass, service_calls)
|
||||
== state["condition_true"]
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_experimental_triggers_conditions")
|
||||
@@ -207,17 +193,17 @@ async def test_light_state_condition_behavior_any(
|
||||
parametrize_target_entities("light"),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("condition", "target_state", "other_state"),
|
||||
("condition", "condition_options", "states"),
|
||||
[
|
||||
(
|
||||
"light.is_on",
|
||||
{"state": STATE_ON, "attributes": {}},
|
||||
{"state": STATE_OFF, "attributes": {}},
|
||||
*parametrize_condition_states(
|
||||
condition="light.is_on",
|
||||
target_states=[STATE_ON],
|
||||
other_states=[STATE_OFF],
|
||||
),
|
||||
(
|
||||
"light.is_off",
|
||||
{"state": STATE_OFF, "attributes": {}},
|
||||
{"state": STATE_ON, "attributes": {}},
|
||||
*parametrize_condition_states(
|
||||
condition="light.is_off",
|
||||
target_states=[STATE_OFF],
|
||||
other_states=[STATE_ON],
|
||||
),
|
||||
],
|
||||
)
|
||||
@@ -229,8 +215,8 @@ async def test_light_state_condition_behavior_all(
|
||||
entity_id: str,
|
||||
entities_in_target: int,
|
||||
condition: str,
|
||||
target_state: str,
|
||||
other_state: str,
|
||||
condition_options: dict[str, Any],
|
||||
states: list[ConditionStateDescription],
|
||||
) -> None:
|
||||
"""Test the light state condition with the 'all' behavior."""
|
||||
# Set state for two switches to ensure that they don't impact the condition
|
||||
@@ -241,7 +227,7 @@ async def test_light_state_condition_behavior_all(
|
||||
|
||||
# Set all lights, including the tested light, to the initial state
|
||||
for eid in target_lights:
|
||||
set_or_remove_state(hass, eid, other_state)
|
||||
set_or_remove_state(hass, eid, states[0]["included"])
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await setup_automation_with_light_condition(
|
||||
@@ -251,27 +237,22 @@ async def test_light_state_condition_behavior_all(
|
||||
behavior="all",
|
||||
)
|
||||
|
||||
# No lights on the condition state
|
||||
assert not await has_call_after_trigger(hass, service_calls)
|
||||
for state in states:
|
||||
included_state = state["included"]
|
||||
|
||||
# Set one light to the condition state -> condition fail
|
||||
set_or_remove_state(hass, entity_id, target_state)
|
||||
assert await has_call_after_trigger(hass, service_calls) == (
|
||||
entities_in_target == 1
|
||||
)
|
||||
set_or_remove_state(hass, entity_id, included_state)
|
||||
await hass.async_block_till_done()
|
||||
# The condition passes if all entities are either in a target state or invalid
|
||||
assert await has_single_call_after_trigger(hass, service_calls) == (
|
||||
(not state["state_valid"])
|
||||
or (state["condition_true"] and entities_in_target == 1)
|
||||
)
|
||||
|
||||
# Set all remaining lights to the condition state -> condition pass
|
||||
for eid in other_entity_ids:
|
||||
set_or_remove_state(hass, eid, target_state)
|
||||
assert await has_call_after_trigger(hass, service_calls)
|
||||
for other_entity_id in other_entity_ids:
|
||||
set_or_remove_state(hass, other_entity_id, included_state)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
for invalid_state in INVALID_STATES:
|
||||
# Set one light to the invalid state -> condition still pass
|
||||
set_or_remove_state(hass, entity_id, invalid_state)
|
||||
assert await has_call_after_trigger(hass, service_calls)
|
||||
|
||||
for invalid_state in INVALID_STATES:
|
||||
# Set all lights to unavailable -> condition passes
|
||||
for eid in other_entity_ids:
|
||||
set_or_remove_state(hass, eid, invalid_state)
|
||||
assert await has_call_after_trigger(hass, service_calls)
|
||||
# The condition passes if all entities are either in a target state or invalid
|
||||
assert await has_single_call_after_trigger(hass, service_calls) == (
|
||||
(not state["state_valid"]) or state["condition_true"]
|
||||
)
|
||||
|
||||
@@ -96,7 +96,8 @@ async def integration_fixture(
|
||||
"eve_energy_20ecn4101",
|
||||
"eve_energy_plug",
|
||||
"eve_energy_plug_patched",
|
||||
"eve_thermo",
|
||||
"eve_thermo_v4",
|
||||
"eve_thermo_v5",
|
||||
"eve_shutter",
|
||||
"eve_weather_sensor",
|
||||
"extended_color_light",
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user