66
77from homeassistant .components .binary_sensor import BinarySensorEntity
88from homeassistant .config_entries import ConfigEntry
9- from homeassistant .const import EVENT_STATE_CHANGED , EVENT_STATE_REPORTED
9+ from homeassistant .const import EVENT_STATE_CHANGED
1010from homeassistant .core import Event , HomeAssistant , callback
1111from homeassistant .helpers .entity_platform import AddConfigEntryEntitiesCallback
1212from homeassistant .helpers .event import (
@@ -93,51 +93,27 @@ async def async_added_to_hass(self) -> None:
9393 self ._hass .bus .async_listen (EVENT_STATE_CHANGED , self ._state_changed )
9494 )
9595
96- # self.async_on_remove(
97- # async_track_state_report_event(
98- # self.hass,
99- # [self._calendar_entity_id],
100- # self.async_state_reported,
101- # )
102- # )
103-
104- # await self._update_state()
96+ await self ._update_state ()
10597
10698 @callback
10799 async def _state_changed (self , event : Event ) -> None :
108100 """Handle calendar entity state changes."""
109101 if event .data .get ("entity_id" ) == self ._calendar_entity_id :
102+ print (event )
110103 await self ._update_state ()
111104
112- @callback
113- async def async_state_reported (
114- self , event : Event [EventStateReportedData ] | None = None
115- ) -> None :
116- """Handle calendar entity state updates."""
117-
118- # This gets fired every minute, but not at startup
119- print (event )
120-
121- if event is None or event .data is None :
122- return
123-
124- state = event .data .get ("new_state" )
125-
126- if state and state .entity_id != self ._calendar_entity_id :
127- return
105+ async def _update_state (self ) -> None :
106+ """Update the binary sensor state based on calendar events."""
128107
129- if state .state == "off" :
130- return
108+ calendar_state = self ._hass .states .get (self ._calendar_entity_id )
131109
132- summary = state .attributes .get ("message" )
133- if not summary :
134- return
110+ print (calendar_state )
135111
136- if self . _summary . lower () in str ( summary ). lower () :
137- self ._attr_is_on = True
112+ if calendar_state is None :
113+ self ._attr_is_on = False
138114 self ._attr_extra_state_attributes .update (
139115 {
140- ATTR_DESCRIPTION : state . attributes . get ( "description" , "" ) ,
116+ ATTR_DESCRIPTION : None ,
141117 }
142118 )
143119 self .async_write_ha_state ()
@@ -151,37 +127,25 @@ async def async_state_reported(
151127 ATTR_DESCRIPTION : event .get ("description" , "" ),
152128 }
153129 )
154-
155- self .async_write_ha_state ()
156-
157- async def _update_state (self ) -> None :
158- """Update the binary sensor state based on calendar events."""
159-
160- calendar_state = self ._hass .states .get (self ._calendar_entity_id )
161-
162- if calendar_state is None :
130+ else :
163131 self ._attr_is_on = False
164132 self ._attr_extra_state_attributes .update (
165133 {
166134 ATTR_DESCRIPTION : None ,
167135 }
168136 )
169- self .async_write_ha_state ()
170- return
171137
172- # TODO: If state is on, then we need to check every minute in case our event is not the one that turned it on
138+ self . async_write_ha_state ()
173139
174- event = await self . _get_event_matching_summary ()
175- if event :
176- self . _attr_is_on = True
177- self . _attr_extra_state_attributes . update (
178- {
179- ATTR_DESCRIPTION : event . get ( "description" , "" ) ,
180- }
140+ # TODO: If state is on, then we need to check every minute in case our event is not the one that turned it on
141+ if calendar_state . state == "on" :
142+ now = datetime . now ()
143+ seconds_until_next_minute = 60 - now . second
144+ self . _hass . loop . call_later (
145+ seconds_until_next_minute ,
146+ lambda : self . _hass . async_create_task ( self . _update_state ()),
181147 )
182148
183- self .async_write_ha_state ()
184-
185149 async def _get_event_matching_summary (self ) -> Event | None :
186150 """Check if the summary is in the calendar events."""
187151
0 commit comments