Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a display switch to toggle ac display #23

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion custom_components/miraie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .const import DOMAIN

# For your initial PR, limit it to 1 platform.
PLATFORMS: list[Platform] = [Platform.CLIMATE]
PLATFORMS: list[Platform] = [Platform.CLIMATE, Platform.SWITCH]

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up mirAIe from a config entry."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/miraie/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"@Kir4Kun"
],
"iot_class": "cloud_push",
"version": "1.0.7"
"version": "1.0.8"
}
99 changes: 99 additions & 0 deletions custom_components/miraie/switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""The MirAIe climate platform."""

from __future__ import annotations
from typing import Any
from miraie_ac import (
Device as MirAIeDevice,
MirAIeHub,
DisplayMode,
)

from homeassistant.components.switch import (
SwitchEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import (
DOMAIN,
)

from .logger import LOGGER

async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:

"""Set up the MirAIe Climate Hub."""
hub: MirAIeHub = hass.data[DOMAIN][entry.entry_id]

entities = list(map(MirAIeDisplaySwitch, hub.home.devices))

async_add_entities(entities)


class MirAIeDisplaySwitch(SwitchEntity):
"""Representation of a MirAIe Climate."""

def __init__(self, device: MirAIeDevice) -> None:
self._attr_should_poll: bool = False
self._attr_unique_id = device.id
self.device = device

@property
def name(self) -> str:
"""Return the display name of this switch."""
return f"{self.device.friendly_name} Display"

@property
def translation_key(self) -> str:
"""Return the translation key."""
return DOMAIN

@property
def icon(self) -> str | None:
"""Return the icon to use in the frontend, if any."""
return "mdi:eye-outline" if self.is_on else "mdi:eye-off-outline"

@property
def device_info(self) -> DeviceInfo:
"""Return the device info."""
return DeviceInfo(
identifiers={
# Serial numbers are unique identifiers within a specific domain
(DOMAIN, self.device.id)
},
name=self.device.friendly_name,
manufacturer=self.device.details.brand,
model=self.device.details.model_number,
sw_version=self.device.details.firmware_version,
)

@property
def is_on(self) -> bool:
"""Return True if display is on."""
return self.device.status.display_mode == DisplayMode.ON

async def async_turn_off(self) -> None:
await self.device.set_display_mode(DisplayMode.OFF)

async def async_turn_on(self) -> None:
await self.device.set_display_mode(DisplayMode.ON)

async def async_added_to_hass(self) -> None:
"""Run when this Entity has been added to HA."""

LOGGER.debug("Successfully added display switch to HA")

# Sensors should also register callbacks to HA when their state changes
self.device.register_callback(self.async_write_ha_state)

async def async_will_remove_from_hass(self) -> None:
"""Entity being removed from hass."""

LOGGER.debug("Successfully removed display switch from HA")

# The opposite of async_added_to_hass. Remove any registered call backs here.
self.device.remove_callback(self.async_write_ha_state)