Skip to content

Commit

Permalink
Initial support for lights (on/off).
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanU committed Mar 24, 2023
1 parent 73b1bb6 commit 99cc7aa
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 24 deletions.
3 changes: 2 additions & 1 deletion custom_components/mygekko/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
SENSOR = "sensor"
SWITCH = "switch"
COVER = "cover"
PLATFORMS = [COVER]
LIGHT = "light"
PLATFORMS = [COVER, LIGHT]


# Configuration and options
Expand Down
11 changes: 7 additions & 4 deletions custom_components/mygekko/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from homeassistant.components.cover import ATTR_POSITION, CoverEntity, CoverDeviceClass
from PyMyGekko.resources.Blinds import Blind, BlindState

from .const import DOMAIN
from .const import COVER, DOMAIN
from .entity import MyGekkoEntity


Expand All @@ -24,10 +24,9 @@ class MyGekkoCover(MyGekkoEntity, CoverEntity):
_attr_device_class = CoverDeviceClass.SHUTTER

def __init__(self, coordinator, blind: Blind):
super().__init__(coordinator, blind)
super().__init__(coordinator, blind, COVER)
self._blind = blind

self._attr_current_cover_position = self._blind.position
print(blind.id, blind.name)

@callback
def _handle_coordinator_update(self) -> None:
Expand Down Expand Up @@ -68,3 +67,7 @@ async def async_stop_cover(self, **kwargs: Any):

async def async_set_cover_position(self, **kwargs: Any) -> None:
await self._blind.set_position(float(kwargs[ATTR_POSITION]))

async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
"""Move the cover tilt to a specific position."""
await ...
22 changes: 6 additions & 16 deletions custom_components/mygekko/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@


class MyGekkoEntity(CoordinatorEntity):
_attr_name = None
_attr_has_entity_name = True
def __init__(self, coordinator, entity: Entity):

def __init__(self, coordinator, entity: Entity, platform: str):
super().__init__(coordinator)
self.entity = entity

@property
def unique_id(self):
"""Return a unique ID to use for this entity."""
return self.entity.id
self._attr_unique_id = platform + "_" + self.entity.id
print(self.unique_id)

@property
def device_info(self):
Expand All @@ -26,13 +25,4 @@ def device_info(self):
"name": self.entity.name,
"model": VERSION,
"manufacturer": NAME,
}

@property
def device_state_attributes(self):
"""Return the state attributes."""
return {
"attribution": ATTRIBUTION,
"id": "cover_" + str(self.entity.id),
"integration": DOMAIN,
}
}
44 changes: 44 additions & 0 deletions custom_components/mygekko/light.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Light platform for MyGekko."""
from homeassistant.core import callback
from homeassistant.components.light import LightEntity, ColorMode
from PyMyGekko.resources.Lights import Light, LightState

from .const import DOMAIN, LIGHT
from .entity import MyGekkoEntity


async def async_setup_entry(hass, entry, async_add_devices):
"""Setup light platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]
lights = coordinator.api.get_lights()
print(lights)
if lights is not None:
async_add_devices(MyGekkoLight(coordinator, light) for light in lights)


class MyGekkoLight(MyGekkoEntity, LightEntity):
"""mygekko Light class."""


def __init__(self, coordinator, light: Light):
super().__init__(coordinator, light, LIGHT)
print(light.id, light.name)
self._light = light
self._attr_supported_color_modes = set()
self._attr_supported_color_modes.add(ColorMode.ONOFF)
self._attr_color_mode = ColorMode.ONOFF


@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self.async_write_ha_state()

def is_on(self) -> bool | None:
return self._light.state == LightState.ON

async def async_turn_off(self, **kwargs):
await self._light.set_state(LightState.OFF)

async def async_turn_on(self, **kwargs):
await self._light.set_state(LightState.ON)
7 changes: 4 additions & 3 deletions custom_components/mygekko/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
"binary_sensor": "Binary sensor enabled",
"sensor": "Sensor enabled",
"cover": "Cover enabled",
"switch": "Switch enabled"
},
"demo_mode": "Demo Mode"
"light": "Light enabled",
"switch": "Switch enabled",
"demo_mode": "Demo Mode"
}
}
}
}
Expand Down

0 comments on commit 99cc7aa

Please sign in to comment.