hass-core/homeassistant/components/volvooncall/switch.py
y34hbuddy b5a6ee3c56
Refactor volvooncall to (mostly) use DataUpdateCoordinator (#75885)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2022-08-04 19:44:39 +02:00

51 lines
1.5 KiB
Python

"""Support for Volvo heater."""
from __future__ import annotations
from homeassistant.components.switch import SwitchEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DATA_KEY, VolvoEntity, VolvoUpdateCoordinator
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up a Volvo switch."""
if discovery_info is None:
return
async_add_entities([VolvoSwitch(hass.data[DATA_KEY], *discovery_info)])
class VolvoSwitch(VolvoEntity, SwitchEntity):
"""Representation of a Volvo switch."""
def __init__(
self,
coordinator: VolvoUpdateCoordinator,
vin: str,
component: str,
attribute: str,
slug_attr: str,
) -> None:
"""Initialize the switch."""
super().__init__(vin, component, attribute, slug_attr, coordinator)
@property
def is_on(self):
"""Determine if switch is on."""
return self.instrument.state
async def async_turn_on(self, **kwargs):
"""Turn the switch on."""
await self.instrument.turn_on()
await self.coordinator.async_request_refresh()
async def async_turn_off(self, **kwargs):
"""Turn the switch off."""
await self.instrument.turn_off()
await self.coordinator.async_request_refresh()